1 /* $NetBSD: mms.c,v 1.18 2021/04/24 23:36:31 thorpej 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 * 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: mms.c,v 1.18 2021/04/24 23:36:31 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/device.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 40 #include "wsmouse.h" 41 42 #include <dev/wscons/wsconsio.h> 43 #include <dev/wscons/wsmousevar.h> 44 45 #include <dreamcast/dev/maple/maple.h> 46 #include <dreamcast/dev/maple/mapleconf.h> 47 48 struct mms_condition { 49 uint32_t func_code; /* function code (big endian) */ 50 uint32_t buttons; 51 uint16_t axis1; /* X */ 52 uint16_t axis2; /* Y */ 53 uint16_t axis3; /* wheel */ 54 uint16_t axis4; 55 uint16_t axis5; 56 uint16_t axis6; 57 uint16_t axis7; 58 uint16_t axis8; 59 }; 60 61 #define MMS_BUTTON_C 0x01 /* middle */ 62 #define MMS_BUTTON_B 0x02 /* right */ 63 #define MMS_BUTTON_A 0x04 /* left */ 64 #define MMS_BUTTON_START 0x08 /* thumb */ 65 #define MMS_BUTTON_MASK 0x0f 66 67 #define MMS_MOVEMENT_BASE 0x200 68 #define MMS_MOVEMENT_MAX 0x3ff 69 70 #define MMS_FUNCDATA_AXIS1 0x001 71 #define MMS_FUNCDATA_AXIS2 0x002 72 #define MMS_FUNCDATA_AXIS3 0x004 73 #define MMS_FUNCDATA_AXIS4 0x008 74 #define MMS_FUNCDATA_AXIS5 0x010 75 #define MMS_FUNCDATA_AXIS6 0x020 76 #define MMS_FUNCDATA_AXIS7 0x040 77 #define MMS_FUNCDATA_AXIS8 0x080 78 #define MMS_FUNCDATA_C 0x100 79 #define MMS_FUNCDATA_B 0x200 80 #define MMS_FUNCDATA_A 0x400 81 #define MMS_FUNCDATA_START 0x800 82 83 struct mms_softc { 84 device_t sc_dev; 85 86 device_t sc_parent; 87 struct maple_unit *sc_unit; 88 89 uint32_t sc_oldbuttons; 90 91 device_t sc_wsmousedev; 92 }; 93 94 static int mms_match(device_t, cfdata_t, void *); 95 static void mms_attach(device_t, device_t, void *); 96 static int mms_detach(device_t, int); 97 98 CFATTACH_DECL_NEW(mms, sizeof(struct mms_softc), 99 mms_match, mms_attach, mms_detach, NULL); 100 101 static int mms_enable(void *); 102 static int mms_ioctl(void *, u_long, void *, int, struct lwp *); 103 static void mms_disable(void *); 104 105 const struct wsmouse_accessops mms_accessops = { 106 mms_enable, 107 mms_ioctl, 108 mms_disable, 109 }; 110 111 static void mms_intr(void *, struct maple_response *, int, int); 112 113 static int 114 mms_match(device_t parent, cfdata_t cf, void *aux) 115 { 116 struct maple_attach_args *ma = aux; 117 118 return ma->ma_function == MAPLE_FN_MOUSE ? MAPLE_MATCH_FUNC : 0; 119 } 120 121 static void 122 mms_attach(device_t parent, device_t self, void *aux) 123 { 124 struct mms_softc *sc = device_private(self); 125 struct maple_attach_args *ma = aux; 126 struct wsmousedev_attach_args a; 127 uint32_t data; 128 129 printf(": SEGA Dreamcast Mouse\n"); 130 131 sc->sc_dev = self; 132 sc->sc_parent = parent; 133 sc->sc_unit = ma->ma_unit; 134 135 data = maple_get_function_data(ma->ma_devinfo, 136 MAPLE_FN_MOUSE); 137 138 printf("%s: buttons:", device_xname(self)); 139 if (data & MMS_FUNCDATA_A) 140 printf(" left"); 141 if (data & MMS_FUNCDATA_C) 142 printf(" middle"); 143 if (data & MMS_FUNCDATA_B) 144 printf(" right"); 145 if (data & MMS_FUNCDATA_START) 146 printf(" thumb"); 147 printf("\n"); 148 149 sc->sc_oldbuttons = 0; 150 151 a.accessops = &mms_accessops; 152 a.accesscookie = sc; 153 154 /* 155 * Attach the mouse, saving a handle to it. 156 */ 157 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARG_EOL); 158 if (sc->sc_wsmousedev == NULL) { 159 /* Nothing more to do here. */ 160 return; 161 } 162 163 maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MOUSE, mms_intr, sc); 164 } 165 166 static int 167 mms_detach(device_t self, int flags) 168 { 169 struct mms_softc *sc = device_private(self); 170 int rv = 0; 171 172 if (sc->sc_wsmousedev != NULL) 173 rv = config_detach(sc->sc_wsmousedev, flags); 174 175 return rv; 176 } 177 178 static int 179 mms_enable(void *v) 180 { 181 struct mms_softc *sc = v; 182 183 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 1); 184 return 0; 185 } 186 187 static void 188 mms_disable(void *v) 189 { 190 struct mms_softc *sc = v; 191 192 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 0); 193 } 194 195 static int 196 mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 197 { 198 199 switch (cmd) { 200 case WSMOUSEIO_GTYPE: 201 *(u_int *) data = WSMOUSE_TYPE_MAPLE; 202 break; 203 204 case WSMOUSEIO_SRES: 205 /* XXX */ 206 return EOPNOTSUPP; 207 208 default: 209 return EPASSTHROUGH; 210 } 211 212 return 0; 213 } 214 215 static void 216 mms_intr(void *arg, struct maple_response *response, int size, int flags) 217 { 218 struct mms_softc *sc = arg; 219 struct mms_condition *data = (void *) response->data; 220 int dx = 0, dy = 0, dz = 0, buttons = 0; 221 uint32_t buttonchg; 222 223 if ((flags & MAPLE_FLAG_PERIODIC) == 0 || 224 size < sizeof(*data)) 225 return; 226 227 data->buttons &= MMS_BUTTON_MASK; 228 buttonchg = sc->sc_oldbuttons ^ data->buttons; 229 sc->sc_oldbuttons = data->buttons; 230 231 dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 232 dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 233 dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE; 234 235 if (dx || dy || dz || buttonchg) { 236 if ((data->buttons & MMS_BUTTON_A) == 0) 237 buttons |= 0x01; 238 if ((data->buttons & MMS_BUTTON_C) == 0) 239 buttons |= 0x02; 240 if ((data->buttons & MMS_BUTTON_B) == 0) 241 buttons |= 0x04; 242 if ((data->buttons & MMS_BUTTON_START) == 0) 243 buttons |= 0x08; 244 245 wsmouse_input(sc->sc_wsmousedev, 246 buttons, 247 dx, -dy, dz, 0, 248 WSMOUSE_INPUT_DELTA); 249 } 250 } 251