1 /* $NetBSD: mkbd.c,v 1.33 2025/11/24 08:17:29 nia Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 Marcus Comstedt 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of The NetBSD Foundation nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 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: mkbd.c,v 1.33 2025/11/24 08:17:29 nia Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/device.h> 37 #include <sys/fcntl.h> 38 #include <sys/poll.h> 39 #include <sys/select.h> 40 #include <sys/proc.h> 41 #include <sys/signalvar.h> 42 #include <sys/systm.h> 43 #include <sys/bus.h> 44 45 #include "wskbd.h" 46 47 #include <dev/wscons/wsconsio.h> 48 #include <dev/wscons/wskbdvar.h> 49 #include <dev/wscons/wsksymdef.h> 50 #include <dev/wscons/wsksymvar.h> 51 52 #include <machine/cpu.h> 53 54 #include <dreamcast/dev/maple/maple.h> 55 #include <dreamcast/dev/maple/mapleconf.h> 56 #include <dreamcast/dev/maple/mkbdvar.h> 57 #include <dreamcast/dev/maple/mkbdmap.h> 58 59 /* 60 * Function declarations. 61 */ 62 static int mkbdmatch(device_t, cfdata_t, void *); 63 static void mkbdattach(device_t, device_t, void *); 64 static int mkbddetach(device_t, int); 65 66 int mkbd_enable(void *, int); 67 void mkbd_set_leds(void *, int); 68 int mkbd_ioctl(void *, u_long, void *, int, struct lwp *); 69 70 struct wskbd_accessops mkbd_accessops = { 71 mkbd_enable, 72 mkbd_set_leds, 73 mkbd_ioctl, 74 }; 75 76 static void mkbd_intr(void *, struct maple_response *, int, int); 77 78 void mkbd_cngetc(void *, u_int *, int *); 79 void mkbd_cnpollc(void *, int); 80 int mkbd_cnattach(void); 81 82 struct wskbd_consops mkbd_consops = { 83 mkbd_cngetc, 84 mkbd_cnpollc, 85 }; 86 87 struct wskbd_mapdata mkbd_keymapdata = { 88 mkbd_keydesctab, 89 KB_JP, 90 }; 91 92 static struct mkbd_softc *mkbd_console_softc; 93 94 static int mkbd_is_console; 95 static int mkbd_console_initted; 96 97 CFATTACH_DECL_NEW(mkbd, sizeof(struct mkbd_softc), 98 mkbdmatch, mkbdattach, mkbddetach, NULL); 99 100 static int 101 mkbdmatch(device_t parent, cfdata_t cf, void *aux) 102 { 103 struct maple_attach_args *ma = aux; 104 105 return ma->ma_function == MAPLE_FN_KEYBOARD ? MAPLE_MATCH_FUNC : 0; 106 } 107 108 static void 109 mkbdattach(device_t parent, device_t self, void *aux) 110 { 111 struct mkbd_softc *sc = device_private(self); 112 struct maple_attach_args *ma = aux; 113 #if NWSKBD > 0 114 struct wskbddev_attach_args a; 115 #endif 116 uint32_t kbdtype; 117 118 sc->sc_dev = self; 119 sc->sc_parent = parent; 120 sc->sc_unit = ma->ma_unit; 121 122 kbdtype = maple_get_function_data(ma->ma_devinfo, 123 MAPLE_FN_KEYBOARD) >> 24; 124 switch (kbdtype) { 125 case 1: 126 printf(": Japanese keyboard"); 127 mkbd_keymapdata.layout = KB_JP; 128 break; 129 case 2: 130 printf(": US keyboard"); 131 mkbd_keymapdata.layout = KB_US; 132 break; 133 case 3: 134 printf(": European keyboard"); 135 mkbd_keymapdata.layout = KB_UK; 136 break; 137 default: 138 printf(": Unknown keyboard %d", kbdtype); 139 } 140 printf("\n"); 141 #ifdef MKBD_LAYOUT 142 /* allow user to override the default keymap */ 143 mkbd_keymapdata.layout = MKBD_LAYOUT; 144 #endif 145 #ifdef MKBD_SWAPCTRLCAPS 146 /* allow user to specify swapctrlcaps with the default keymap */ 147 mkbd_keymapdata.layout |= KB_SWAPCTRLCAPS; 148 #endif 149 150 #if NWSKBD > 0 151 if ((a.console = mkbd_is_console) != 0) { 152 mkbd_is_console = 0; 153 if (!mkbd_console_initted) 154 wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata); 155 mkbd_console_softc = sc; 156 } 157 a.keymap = &mkbd_keymapdata; 158 a.accessops = &mkbd_accessops; 159 a.accesscookie = sc; 160 sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE); 161 #endif 162 163 maple_set_callback(parent, sc->sc_unit, MAPLE_FN_KEYBOARD, 164 mkbd_intr, sc); 165 maple_enable_periodic(parent, sc->sc_unit, MAPLE_FN_KEYBOARD, 1); 166 } 167 168 static int 169 mkbddetach(device_t self, int flags) 170 { 171 struct mkbd_softc *sc = device_private(self); 172 int rv = 0; 173 174 if (sc == mkbd_console_softc) { 175 /* 176 * Hack to allow another Maple keyboard to be new console. 177 * XXX Should some other type device can be console. 178 */ 179 printf("%s: was console keyboard\n", device_xname(sc->sc_dev)); 180 wskbd_cndetach(); 181 mkbd_console_softc = NULL; 182 mkbd_console_initted = 0; 183 mkbd_is_console = 1; 184 } 185 if (sc->sc_wskbddev) 186 rv = config_detach(sc->sc_wskbddev, flags); 187 188 return rv; 189 } 190 191 int 192 mkbd_enable(void *v, int on) 193 { 194 195 return 0; 196 } 197 198 void 199 mkbd_set_leds(void *v, int on) 200 { 201 } 202 203 int 204 mkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 205 { 206 207 switch (cmd) { 208 case WSKBDIO_GTYPE: 209 *(int *) data = WSKBD_TYPE_MAPLE; 210 return 0; 211 case WSKBDIO_SETLEDS: 212 return 0; 213 case WSKBDIO_GETLEDS: 214 *(int *) data = 0; 215 return 0; 216 case WSKBDIO_COMPLEXBELL: 217 return 0; 218 } 219 220 return EPASSTHROUGH; 221 } 222 223 int 224 mkbd_cnattach(void) 225 { 226 227 wskbd_cnattach(&mkbd_consops, NULL, &mkbd_keymapdata); 228 mkbd_console_initted = 1; 229 mkbd_is_console = 1; 230 231 return 0; 232 } 233 234 static int polledkey; 235 extern int maple_polling; 236 237 #define SHIFT_KEYCODE_BASE 0xe0 238 #define UP_KEYCODE_FLAG 0x1000 239 240 #define KEY_UP(n) do { \ 241 if (maple_polling) \ 242 polledkey = (n)|UP_KEYCODE_FLAG; \ 243 else \ 244 wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, (n)); \ 245 } while (/*CONSTCOND*/0) 246 247 #define KEY_DOWN(n) do { \ 248 if (maple_polling) \ 249 polledkey = (n); \ 250 else \ 251 wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, (n)); \ 252 } while (/*CONSTCOND*/0) 253 254 #define SHIFT_UP(n) KEY_UP((n) | SHIFT_KEYCODE_BASE) 255 #define SHIFT_DOWN(n) KEY_DOWN((n) | SHIFT_KEYCODE_BASE) 256 257 static void 258 mkbd_intr(void *arg, struct maple_response *response, int sz, int flags) 259 { 260 struct mkbd_softc *sc = arg; 261 struct mkbd_condition *kbddata = (void *) response->data; 262 263 if ((flags & MAPLE_FLAG_PERIODIC) && 264 sz >= sizeof(struct mkbd_condition)) { 265 int i, j, v; 266 267 v = sc->sc_condition.shift & ~kbddata->shift; 268 if (v) 269 for (i = 0; i < 8; i++) 270 if (v & (1 << i)) 271 SHIFT_UP(i); 272 273 v = kbddata->shift & ~sc->sc_condition.shift; 274 if (v) 275 for (i = 0; i < 8; i++) 276 if (v & (1 << i)) 277 SHIFT_DOWN(i); 278 279 for (i = 0, j = 0; i < 6; i++) 280 if (sc->sc_condition.key[i] < 4) 281 break; 282 else if (sc->sc_condition.key[i] == kbddata->key[j]) 283 j++; 284 else 285 KEY_UP(sc->sc_condition.key[i]); 286 287 for (; j < 6; j++) 288 if (kbddata->key[j] < 4) 289 break; 290 else 291 KEY_DOWN(kbddata->key[j]); 292 293 memcpy(&sc->sc_condition, kbddata, 294 sizeof(struct mkbd_condition)); 295 } 296 } 297 298 void 299 mkbd_cngetc(void *v, u_int *type, int *data) 300 { 301 int key; 302 303 polledkey = -1; 304 maple_polling = 1; 305 while (polledkey == -1) { 306 if (mkbd_console_softc != NULL && 307 mkbd_console_softc->sc_parent != NULL) { 308 DELAY(20000); 309 maple_run_polling(mkbd_console_softc->sc_parent); 310 } 311 } 312 maple_polling = 0; 313 key = polledkey; 314 315 *data = key & ~UP_KEYCODE_FLAG; 316 *type = (key & UP_KEYCODE_FLAG) ? 317 WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 318 } 319 320 void 321 mkbd_cnpollc(void *v, int on) 322 { 323 } 324