1/* $NetBSD: akbd.c,v 1.27 2021/09/18 15:14:40 tsutsui Exp $ */ 2 3/* 4 * Copyright (C) 1998 Colin Wood 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Colin Wood. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: akbd.c,v 1.27 2021/09/18 15:14:40 tsutsui Exp $"); 35 36#include "opt_adb.h" 37 38#include <sys/param.h> 39#include <sys/device.h> 40#include <sys/fcntl.h> 41#include <sys/poll.h> 42#include <sys/select.h> 43#include <sys/proc.h> 44#include <sys/signalvar.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47 48#include "aed.h" 49#include "wskbd.h" 50 51#include <dev/wscons/wsconsio.h> 52#include <dev/wscons/wskbdvar.h> 53#include <dev/wscons/wsksymdef.h> 54#include <dev/wscons/wsksymvar.h> 55 56#include <machine/autoconf.h> 57#include <machine/cpu.h> 58#define KEYBOARD_ARRAY 59#include <machine/keyboard.h> 60#include <machine/viareg.h> 61 62#include <mac68k/mac68k/macrom.h> 63#include <mac68k/dev/adbvar.h> 64#include <mac68k/dev/aedvar.h> 65#include <mac68k/dev/akbdmap.h> 66#include <mac68k/dev/akbdvar.h> 67#include <mac68k/dev/amsvar.h> 68 69/* 70 * Function declarations. 71 */ 72static int akbdmatch(device_t, cfdata_t, void *); 73static void akbdattach(device_t, device_t, void *); 74static void kbd_processevent(adb_event_t *, struct akbd_softc *); 75#ifdef notyet 76static u_char getleds(int); 77static int setleds(struct akbd_softc *, u_char); 78static void blinkleds(struct akbd_softc *); 79#endif 80 81/* 82 * Local variables. 83 */ 84 85/* Driver definition. */ 86CFATTACH_DECL_NEW(akbd, sizeof(struct akbd_softc), 87 akbdmatch, akbdattach, NULL, NULL); 88 89extern struct cfdriver akbd_cd; 90 91int kbd_intr(adb_event_t *, struct akbd_softc *); 92int akbd_enable(void *, int); 93void akbd_set_leds(void *, int); 94int akbd_ioctl(void *, u_long, void *, int, struct lwp *); 95 96struct wskbd_accessops akbd_accessops = { 97 akbd_enable, 98 akbd_set_leds, 99 akbd_ioctl, 100}; 101 102void akbd_cngetc(void *, u_int *, int *); 103void akbd_cnpollc(void *, int); 104int akbd_cnattach(void); 105 106struct wskbd_consops akbd_consops = { 107 akbd_cngetc, 108 akbd_cnpollc, 109}; 110 111struct wskbd_mapdata akbd_keymapdata = { 112 akbd_keydesctab, 113 KB_US, 114}; 115 116static int akbd_is_console(void); 117 118static int 119akbdmatch(device_t parent, cfdata_t cf, void *aux) 120{ 121 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 122 123 if (aa_args->origaddr == ADBADDR_KBD) 124 return 1; 125 else 126 return 0; 127} 128 129static void 130akbdattach(device_t parent, device_t self, void *aux) 131{ 132 ADBSetInfoBlock adbinfo; 133 struct akbd_softc *sc = device_private(self); 134 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 135 int error __unused, kbd_done; 136 short cmd; 137 u_char buffer[9]; 138#if NWSKBD > 0 139 struct wskbddev_attach_args a; 140 static int akbd_console_initted; 141 int wskbd_eligible; 142 143 wskbd_eligible = 1; 144#endif 145 146 sc->origaddr = aa_args->origaddr; 147 sc->adbaddr = aa_args->adbaddr; 148 sc->handler_id = aa_args->handler_id; 149 150 sc->sc_leds = (u_int8_t)0x00; /* initially off */ 151 152 adbinfo.siServiceRtPtr = (Ptr)adb_kbd_asmcomplete; 153 adbinfo.siDataAreaAddr = (void *)sc; 154 155 switch (sc->handler_id) { 156 case ADB_STDKBD: 157 printf("standard keyboard\n"); 158 break; 159 case ADB_ISOKBD: 160 printf("standard keyboard (ISO layout)\n"); 161 break; 162 case ADB_EXTKBD: 163 cmd = ADBTALK(sc->adbaddr, 1); 164 kbd_done = 165 (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0); 166 167 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */ 168 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) { 169 printf("Mouseman (non-EMP) pseudo keyboard\n"); 170 adbinfo.siServiceRtPtr = (Ptr)0; 171 adbinfo.siDataAreaAddr = (Ptr)0; 172#if NWSKBD > 0 173 wskbd_eligible = 0; 174#endif /* NWSKBD > 0 */ 175 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) { 176 printf("Trackman (non-EMP) pseudo keyboard\n"); 177 adbinfo.siServiceRtPtr = (Ptr)0; 178 adbinfo.siDataAreaAddr = (Ptr)0; 179#if NWSKBD > 0 180 wskbd_eligible = 0; 181#endif /* NWSKBD > 0 */ 182 } else { 183 printf("extended keyboard\n"); 184#ifdef notyet 185 blinkleds(sc); 186#endif 187 } 188 break; 189 case ADB_EXTISOKBD: 190 printf("extended keyboard (ISO layout)\n"); 191#ifdef notyet 192 blinkleds(sc); 193#endif 194 break; 195 case ADB_KBDII: 196 printf("keyboard II\n"); 197 break; 198 case ADB_ISOKBDII: 199 printf("keyboard II (ISO layout)\n"); 200 break; 201 case ADB_PBKBD: 202 printf("PowerBook keyboard\n"); 203 break; 204 case ADB_PBISOKBD: 205 printf("PowerBook keyboard (ISO layout)\n"); 206 break; 207 case ADB_ADJKPD: 208 printf("adjustable keypad\n"); 209#if NWSKBD > 0 210 wskbd_eligible = 0; 211#endif /* NWSKBD > 0 */ 212 break; 213 case ADB_ADJKBD: 214 printf("adjustable keyboard\n"); 215 break; 216 case ADB_ADJISOKBD: 217 printf("adjustable keyboard (ISO layout)\n"); 218 break; 219 case ADB_ADJJAPKBD: 220 printf("adjustable keyboard (Japanese layout)\n"); 221 break; 222 case ADB_PBEXTISOKBD: 223 printf("PowerBook extended keyboard (ISO layout)\n"); 224 break; 225 case ADB_PBEXTJAPKBD: 226 printf("PowerBook extended keyboard (Japanese layout)\n"); 227 break; 228 case ADB_JPKBDII: 229 printf("keyboard II (Japanese layout)\n"); 230 break; 231 case ADB_PBEXTKBD: 232 printf("PowerBook extended keyboard\n"); 233 break; 234 case ADB_DESIGNKBD: 235 printf("extended keyboard\n"); 236#ifdef notyet 237 blinkleds(sc); 238#endif 239 break; 240 case ADB_PBJPKBD: 241 printf("PowerBook keyboard (Japanese layout)\n"); 242 break; 243 default: 244 printf("mapped device (%d)\n", sc->handler_id); 245#if NWSKBD > 0 246 wskbd_eligible = 0; 247#endif /* NWSKBD > 0 */ 248 break; 249 } 250 error = SetADBInfo(&adbinfo, sc->adbaddr); 251#ifdef ADB_DEBUG 252 if (adb_debug) 253 printf("akbd: returned %d from SetADBInfo\n", error); 254#endif 255 256#if NWSKBD > 0 257 if (akbd_is_console() && wskbd_eligible) 258 a.console = (++akbd_console_initted == 1); 259 else 260 a.console = 0; 261 a.keymap = &akbd_keymapdata; 262 a.accessops = &akbd_accessops; 263 a.accesscookie = sc; 264 265 sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE); 266#endif 267} 268 269 270/* 271 * Handle putting the keyboard data received from the ADB into 272 * an ADB event record. 273 */ 274void 275kbd_adbcomplete(uint8_t *buffer, void *data_area, int adb_command) 276{ 277 adb_event_t event; 278 struct akbd_softc *ksc; 279 int adbaddr; 280#ifdef ADB_DEBUG 281 int i; 282 283 if (adb_debug) 284 printf("adb: transaction completion\n"); 285#endif 286 287 adbaddr = ADB_CMDADDR(adb_command); 288 ksc = (struct akbd_softc *)data_area; 289 290 event.addr = adbaddr; 291 event.hand_id = ksc->handler_id; 292 event.def_addr = ksc->origaddr; 293 event.byte_count = buffer[0]; 294 memcpy(event.bytes, buffer + 1, event.byte_count); 295 296#ifdef ADB_DEBUG 297 if (adb_debug) { 298 printf("akbd: from %d at %d (org %d) %d:", event.addr, 299 event.hand_id, event.def_addr, buffer[0]); 300 for (i = 1; i <= buffer[0]; i++) 301 printf(" %x", buffer[i]); 302 printf("\n"); 303 } 304#endif 305 306 microtime(&event.timestamp); 307 308 kbd_processevent(&event, ksc); 309} 310 311/* 312 * Given a keyboard ADB event, record the keycodes and call the key 313 * repeat handler, optionally passing the event through the mouse 314 * button emulation handler first. 315 */ 316static void 317kbd_processevent(adb_event_t *event, struct akbd_softc *ksc) 318{ 319 adb_event_t new_event; 320 321 new_event = *event; 322 new_event.u.k.key = event->bytes[0]; 323 new_event.bytes[1] = 0xff; 324#if NAED > 0 325 if (adb_polling || !aed_input(&new_event)) 326#endif 327#if NWSKBD > 0 328 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 329 kbd_intr(&new_event, ksc); 330#else 331 /* do nothing */ ; 332#endif 333 if (event->bytes[1] != 0xff) { 334 new_event.u.k.key = event->bytes[1]; 335 new_event.bytes[0] = event->bytes[1]; 336 new_event.bytes[1] = 0xff; 337#if NAED > 0 338 if (adb_polling || !aed_input(&new_event)) 339#endif 340#if NWSKBD > 0 341 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 342 kbd_intr(&new_event, ksc); 343#else 344 /* do nothing */ ; 345#endif 346 } 347 348} 349 350#ifdef notyet 351/* 352 * Get the actual hardware LED state and convert it to softc format. 353 */ 354static u_char 355getleds(int addr) 356{ 357 short cmd; 358 u_char buffer[9], leds; 359 360 leds = 0x00; /* all off */ 361 buffer[0] = 0; 362 363 cmd = ADBTALK(addr, 2); 364 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 && 365 buffer[0] > 0) 366 leds = ~(buffer[2]) & 0x07; 367 368 return (leds); 369} 370 371/* 372 * Set the keyboard LED's. 373 * 374 * Automatically translates from ioctl/softc format to the 375 * actual keyboard register format 376 */ 377static int 378setleds(struct akbd_softc *ksc, u_char leds) 379{ 380 int addr; 381 short cmd; 382 u_char buffer[9]; 383 384 if ((leds & 0x07) == (ksc->sc_leds & 0x07)) 385 return (0); 386 387 addr = ksc->adbaddr; 388 buffer[0] = 0; 389 390 cmd = ADBTALK(addr, 2); 391 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0) 392 return (EIO); 393 394 leds = ~leds & 0x07; 395 buffer[2] &= 0xf8; 396 buffer[2] |= leds; 397 398 cmd = ADBLISTEN(addr, 2); 399 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 400 401 /* talk R2 */ 402 cmd = ADBTALK(addr, 2); 403 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0) 404 return (EIO); 405 406 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07; 407 408 if ((buffer[2] & 0xf8) != leds) 409 return (EIO); 410 else 411 return (0); 412} 413 414/* 415 * Toggle all of the LED's on and off, just for show. 416 */ 417static void 418blinkleds(struct akbd_softc *ksc) 419{ 420 int addr, i; 421 u_char blinkleds, origleds; 422 423 addr = ksc->adbaddr; 424 origleds = getleds(addr); 425 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK; 426 427 (void)setleds(ksc, blinkleds); 428 429 for (i = 0; i < 10000; i++) 430 delay(50); 431 432 /* make sure that we restore the LED settings */ 433 i = 10; 434 do { 435 (void)setleds(ksc, (u_char)0x00); 436 } while (setleds(ksc, (u_char)0x00) && (i-- > 0)); 437 438 return; 439} 440#endif 441 442int 443akbd_is_console(void) 444{ 445 extern struct mac68k_machine_S mac68k_machine; 446 447 return ((mac68k_machine.serial_console & 0x03) == 0); 448} 449 450int 451akbd_enable(void *v, int on) 452{ 453 return 0; 454} 455 456void 457akbd_set_leds(void *v, int on) 458{ 459} 460 461int 462akbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 463{ 464 switch (cmd) { 465 466 case WSKBDIO_GTYPE: 467 *(int *)data = WSKBD_TYPE_ADB; 468 return 0; 469 case WSKBDIO_SETLEDS: 470 return 0; 471 case WSKBDIO_GETLEDS: 472 *(int *)data = 0; 473 return 0; 474 case WSKBDIO_COMPLEXBELL: 475#define d ((struct wskbd_bell_data *)data) 476 mac68k_ring_bell(d->pitch, d->period * hz / 1000, 100); 477 /* comes in as msec, goes out as ticks; volume ignored */ 478#undef d 479 return (0); 480 } 481 /* kbdioctl(...); */ 482 483 return EPASSTHROUGH; 484} 485 486static int polledkey; 487extern int adb_polling; 488 489int 490kbd_intr(adb_event_t *event, struct akbd_softc *sc) 491{ 492 int key, press, val; 493 int type; 494 495 key = event->u.k.key; 496 press = ADBK_PRESS(key); 497 val = ADBK_KEYVAL(key); 498 499 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 500 501 if (key == 185) { /* Caps Lock released */ 502 type = WSCONS_EVENT_KEY_DOWN; 503 wskbd_input(sc->sc_wskbddev, type, val); 504 type = WSCONS_EVENT_KEY_UP; 505 } 506 507 if (adb_polling) 508 polledkey = key; 509 else 510 wskbd_input(sc->sc_wskbddev, type, val); 511 512 return 0; 513} 514 515int 516akbd_cnattach(void) 517{ 518 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata); 519 520 return 0; 521} 522 523void 524akbd_cngetc(void *v, u_int *type, int *data) 525{ 526 int intbits, key, press, val; 527 int s; 528 529 s = splhigh(); 530 531 polledkey = -1; 532 adb_polling = 1; 533 534 while (polledkey == -1) { 535 intbits = via_reg(VIA1, vIFR); 536 537 if (intbits & V1IF_ADBRDY) { 538 mrg_adbintr(); 539 via_reg(VIA1, vIFR) = V1IF_ADBRDY; 540 } 541 if (intbits & 0x10) { 542 mrg_pmintr(); 543 via_reg(VIA1, vIFR) = 0x10; 544 } 545 } 546 547 adb_polling = 0; 548 splx(s); 549 550 key = polledkey; 551 press = ADBK_PRESS(key); 552 val = ADBK_KEYVAL(key); 553 554 *data = val; 555 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 556} 557 558void 559akbd_cnpollc(void *v, int on) 560{ 561} 562