1 /* $NetBSD: akbd.c,v 1.28 2025/01/12 05:56:59 nat 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.28 2025/01/12 05:56:59 nat 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 */ 72 static int akbdmatch(device_t, cfdata_t, void *); 73 static void akbdattach(device_t, device_t, void *); 74 static void kbd_processevent(adb_event_t *, struct akbd_softc *); 75 #ifdef notyet 76 static u_char getleds(int); 77 static int setleds(struct akbd_softc *, u_char); 78 static void blinkleds(struct akbd_softc *); 79 #endif 80 81 /* 82 * Local variables. 83 */ 84 85 /* Driver definition. */ 86 CFATTACH_DECL_NEW(akbd, sizeof(struct akbd_softc), 87 akbdmatch, akbdattach, NULL, NULL); 88 89 extern struct cfdriver akbd_cd; 90 91 int kbd_intr(adb_event_t *, struct akbd_softc *); 92 int akbd_enable(void *, int); 93 void akbd_set_leds(void *, int); 94 int akbd_ioctl(void *, u_long, void *, int, struct lwp *); 95 96 struct wskbd_accessops akbd_accessops = { 97 akbd_enable, 98 akbd_set_leds, 99 akbd_ioctl, 100 }; 101 102 void akbd_cngetc(void *, u_int *, int *); 103 void akbd_cnpollc(void *, int); 104 int akbd_cnattach(void); 105 106 struct wskbd_consops akbd_consops = { 107 akbd_cngetc, 108 akbd_cnpollc, 109 }; 110 111 struct wskbd_mapdata akbd_keymapdata = { 112 akbd_keydesctab, 113 KB_US, 114 }; 115 116 static int akbd_is_console(void); 117 118 static int 119 akbdmatch(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 129 static void 130 akbdattach(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 */ 274 void 275 kbd_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 */ 316 static void 317 kbd_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 int result; 326 327 if ((result = aed_input(&new_event)) != 0) 328 return; 329 330 if (adb_polling || !result) 331 #endif 332 #if NWSKBD > 0 333 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 334 kbd_intr(&new_event, ksc); 335 #else 336 /* do nothing */ ; 337 #endif 338 if (event->bytes[1] != 0xff) { 339 new_event.u.k.key = event->bytes[1]; 340 new_event.bytes[0] = event->bytes[1]; 341 new_event.bytes[1] = 0xff; 342 #if NAED > 0 343 if (adb_polling || !aed_input(&new_event)) 344 #endif 345 #if NWSKBD > 0 346 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 347 kbd_intr(&new_event, ksc); 348 #else 349 /* do nothing */ ; 350 #endif 351 } 352 353 } 354 355 #ifdef notyet 356 /* 357 * Get the actual hardware LED state and convert it to softc format. 358 */ 359 static u_char 360 getleds(int addr) 361 { 362 short cmd; 363 u_char buffer[9], leds; 364 365 leds = 0x00; /* all off */ 366 buffer[0] = 0; 367 368 cmd = ADBTALK(addr, 2); 369 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 && 370 buffer[0] > 0) 371 leds = ~(buffer[2]) & 0x07; 372 373 return (leds); 374 } 375 376 /* 377 * Set the keyboard LED's. 378 * 379 * Automatically translates from ioctl/softc format to the 380 * actual keyboard register format 381 */ 382 static int 383 setleds(struct akbd_softc *ksc, u_char leds) 384 { 385 int addr; 386 short cmd; 387 u_char buffer[9]; 388 389 if ((leds & 0x07) == (ksc->sc_leds & 0x07)) 390 return (0); 391 392 addr = ksc->adbaddr; 393 buffer[0] = 0; 394 395 cmd = ADBTALK(addr, 2); 396 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0) 397 return (EIO); 398 399 leds = ~leds & 0x07; 400 buffer[2] &= 0xf8; 401 buffer[2] |= leds; 402 403 cmd = ADBLISTEN(addr, 2); 404 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 405 406 /* talk R2 */ 407 cmd = ADBTALK(addr, 2); 408 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0) 409 return (EIO); 410 411 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07; 412 413 if ((buffer[2] & 0xf8) != leds) 414 return (EIO); 415 else 416 return (0); 417 } 418 419 /* 420 * Toggle all of the LED's on and off, just for show. 421 */ 422 static void 423 blinkleds(struct akbd_softc *ksc) 424 { 425 int addr, i; 426 u_char blinkleds, origleds; 427 428 addr = ksc->adbaddr; 429 origleds = getleds(addr); 430 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK; 431 432 (void)setleds(ksc, blinkleds); 433 434 for (i = 0; i < 10000; i++) 435 delay(50); 436 437 /* make sure that we restore the LED settings */ 438 i = 10; 439 do { 440 (void)setleds(ksc, (u_char)0x00); 441 } while (setleds(ksc, (u_char)0x00) && (i-- > 0)); 442 443 return; 444 } 445 #endif 446 447 int 448 akbd_is_console(void) 449 { 450 extern struct mac68k_machine_S mac68k_machine; 451 452 return ((mac68k_machine.serial_console & 0x03) == 0); 453 } 454 455 int 456 akbd_enable(void *v, int on) 457 { 458 return 0; 459 } 460 461 void 462 akbd_set_leds(void *v, int on) 463 { 464 } 465 466 int 467 akbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 468 { 469 switch (cmd) { 470 471 case WSKBDIO_GTYPE: 472 *(int *)data = WSKBD_TYPE_ADB; 473 return 0; 474 case WSKBDIO_SETLEDS: 475 return 0; 476 case WSKBDIO_GETLEDS: 477 *(int *)data = 0; 478 return 0; 479 case WSKBDIO_COMPLEXBELL: 480 #define d ((struct wskbd_bell_data *)data) 481 mac68k_ring_bell(d->pitch, d->period * hz / 1000, 100); 482 /* comes in as msec, goes out as ticks; volume ignored */ 483 #undef d 484 return (0); 485 } 486 /* kbdioctl(...); */ 487 488 return EPASSTHROUGH; 489 } 490 491 static int polledkey; 492 extern int adb_polling; 493 494 int 495 kbd_intr(adb_event_t *event, struct akbd_softc *sc) 496 { 497 int key, press, val; 498 int type; 499 500 key = event->u.k.key; 501 press = ADBK_PRESS(key); 502 val = ADBK_KEYVAL(key); 503 504 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 505 506 if (key == 185) { /* Caps Lock released */ 507 type = WSCONS_EVENT_KEY_DOWN; 508 wskbd_input(sc->sc_wskbddev, type, val); 509 type = WSCONS_EVENT_KEY_UP; 510 } 511 512 if (adb_polling) 513 polledkey = key; 514 else 515 wskbd_input(sc->sc_wskbddev, type, val); 516 517 return 0; 518 } 519 520 int 521 akbd_cnattach(void) 522 { 523 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata); 524 525 return 0; 526 } 527 528 void 529 akbd_cngetc(void *v, u_int *type, int *data) 530 { 531 int intbits, key, press, val; 532 int s; 533 534 s = splhigh(); 535 536 polledkey = -1; 537 adb_polling = 1; 538 539 while (polledkey == -1) { 540 intbits = via_reg(VIA1, vIFR); 541 542 if (intbits & V1IF_ADBRDY) { 543 mrg_adbintr(); 544 via_reg(VIA1, vIFR) = V1IF_ADBRDY; 545 } 546 if (intbits & 0x10) { 547 mrg_pmintr(); 548 via_reg(VIA1, vIFR) = 0x10; 549 } 550 } 551 552 adb_polling = 0; 553 splx(s); 554 555 key = polledkey; 556 press = ADBK_PRESS(key); 557 val = ADBK_KEYVAL(key); 558 559 *data = val; 560 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 561 } 562 563 void 564 akbd_cnpollc(void *v, int on) 565 { 566 } 567