1 /* $NetBSD: akbd.c,v 1.7 2000/03/19 07:37:58 scottr 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 "opt_adb.h" 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 44 #include "aed.h" 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/autoconf.h> 53 #include <machine/cpu.h> 54 #define KEYBOARD_ARRAY 55 #include <machine/keyboard.h> 56 #include <machine/viareg.h> 57 58 #include <mac68k/mac68k/macrom.h> 59 #include <mac68k/dev/adbvar.h> 60 #include <mac68k/dev/aedvar.h> 61 #include <mac68k/dev/akbdmap.h> 62 #include <mac68k/dev/akbdvar.h> 63 #include <mac68k/dev/amsvar.h> 64 65 /* 66 * Function declarations. 67 */ 68 static int akbdmatch __P((struct device *, struct cfdata *, void *)); 69 static void akbdattach __P((struct device *, struct device *, void *)); 70 void kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command)); 71 static void kbd_processevent __P((adb_event_t *event, struct akbd_softc *)); 72 #ifdef notyet 73 static u_char getleds __P((int)); 74 static int setleds __P((struct akbd_softc *, u_char)); 75 static void blinkleds __P((struct akbd_softc *)); 76 #endif 77 78 /* 79 * Local variables. 80 */ 81 static volatile int kbd_done; /* Did ADBOp() complete? */ 82 83 /* Driver definition. */ 84 struct cfattach akbd_ca = { 85 sizeof(struct akbd_softc), akbdmatch, akbdattach 86 }; 87 88 extern struct cfdriver akbd_cd; 89 90 int kbd_intr __P((adb_event_t *event)); 91 int akbd_enable __P((void *, int)); 92 void akbd_set_leds __P((void *, int)); 93 int akbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *)); 94 95 struct wskbd_accessops akbd_accessops = { 96 akbd_enable, 97 akbd_set_leds, 98 akbd_ioctl, 99 }; 100 101 void akbd_cngetc __P((void *, u_int *, int *)); 102 void akbd_cnpollc __P((void *, int)); 103 int akbd_cnattach __P((void)); 104 105 struct wskbd_consops akbd_consops = { 106 akbd_cngetc, 107 akbd_cnpollc, 108 }; 109 110 struct wskbd_mapdata akbd_keymapdata = { 111 akbd_keydesctab, 112 KB_US, 113 }; 114 115 static int akbd_is_console __P((void)); 116 117 static int 118 akbdmatch(parent, cf, aux) 119 struct device *parent; 120 struct cfdata *cf; 121 void *aux; 122 { 123 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 124 125 if (aa_args->origaddr == ADBADDR_KBD) 126 return 1; 127 else 128 return 0; 129 } 130 131 static void 132 akbdattach(parent, self, aux) 133 struct device *parent, *self; 134 void *aux; 135 { 136 ADBSetInfoBlock adbinfo; 137 struct akbd_softc *sc = (struct akbd_softc *)self; 138 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 139 int count, error; 140 short cmd; 141 u_char buffer[9]; 142 #if NWSKBD > 0 143 struct wskbddev_attach_args a; 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 = (caddr_t)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 kbd_done = 0; 164 cmd = ADBTALK(sc->adbaddr, 1); 165 ADBOp((Ptr)buffer, (Ptr)extdms_complete, 166 (Ptr)&kbd_done, cmd); 167 168 /* Wait until done, but no more than 2 secs */ 169 count = 40000; 170 while (!kbd_done && count-- > 0) 171 delay(50); 172 173 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */ 174 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) { 175 printf("Mouseman (non-EMP) pseudo keyboard\n"); 176 adbinfo.siServiceRtPtr = (Ptr)0; 177 adbinfo.siDataAreaAddr = (Ptr)0; 178 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) { 179 printf("Trackman (non-EMP) pseudo keyboard\n"); 180 adbinfo.siServiceRtPtr = (Ptr)0; 181 adbinfo.siDataAreaAddr = (Ptr)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 break; 210 case ADB_ADJKBD: 211 printf("adjustable keyboard\n"); 212 break; 213 case ADB_ADJISOKBD: 214 printf("adjustable keyboard (ISO layout)\n"); 215 break; 216 case ADB_ADJJAPKBD: 217 printf("adjustable keyboard (Japanese layout)\n"); 218 break; 219 case ADB_PBEXTISOKBD: 220 printf("PowerBook extended keyboard (ISO layout)\n"); 221 break; 222 case ADB_PBEXTJAPKBD: 223 printf("PowerBook extended keyboard (Japanese layout)\n"); 224 break; 225 case ADB_JPKBDII: 226 printf("keyboard II (Japanese layout)\n"); 227 break; 228 case ADB_PBEXTKBD: 229 printf("PowerBook extended keyboard\n"); 230 break; 231 case ADB_DESIGNKBD: 232 printf("extended keyboard\n"); 233 #ifdef notyet 234 blinkleds(sc); 235 #endif 236 break; 237 case ADB_PBJPKBD: 238 printf("PowerBook keyboard (Japanese layout)\n"); 239 break; 240 default: 241 printf("mapped device (%d)\n", sc->handler_id); 242 break; 243 } 244 error = SetADBInfo(&adbinfo, sc->adbaddr); 245 #ifdef ADB_DEBUG 246 if (adb_debug) 247 printf("akbd: returned %d from SetADBInfo\n", error); 248 #endif 249 250 #if NWSKBD > 0 251 a.console = akbd_is_console(); 252 a.keymap = &akbd_keymapdata; 253 a.accessops = &akbd_accessops; 254 a.accesscookie = sc; 255 256 sc->sc_wskbddev = config_found(self, &a, wskbddevprint); 257 #endif 258 } 259 260 261 /* 262 * Handle putting the keyboard data received from the ADB into 263 * an ADB event record. 264 */ 265 void 266 kbd_adbcomplete(buffer, data_area, adb_command) 267 caddr_t buffer; 268 caddr_t data_area; 269 int adb_command; 270 { 271 adb_event_t event; 272 struct akbd_softc *ksc; 273 int adbaddr; 274 #ifdef ADB_DEBUG 275 int i; 276 277 if (adb_debug) 278 printf("adb: transaction completion\n"); 279 #endif 280 281 adbaddr = ADB_CMDADDR(adb_command); 282 ksc = (struct akbd_softc *)data_area; 283 284 event.addr = adbaddr; 285 event.hand_id = ksc->handler_id; 286 event.def_addr = ksc->origaddr; 287 event.byte_count = buffer[0]; 288 memcpy(event.bytes, buffer + 1, event.byte_count); 289 290 #ifdef ADB_DEBUG 291 if (adb_debug) { 292 printf("akbd: from %d at %d (org %d) %d:", event.addr, 293 event.hand_id, event.def_addr, buffer[0]); 294 for (i = 1; i <= buffer[0]; i++) 295 printf(" %x", buffer[i]); 296 printf("\n"); 297 } 298 #endif 299 300 microtime(&event.timestamp); 301 302 kbd_processevent(&event, ksc); 303 } 304 305 /* 306 * Given a keyboard ADB event, record the keycodes and call the key 307 * repeat handler, optionally passing the event through the mouse 308 * button emulation handler first. 309 */ 310 static void 311 kbd_processevent(event, ksc) 312 adb_event_t *event; 313 struct akbd_softc *ksc; 314 { 315 adb_event_t new_event; 316 317 new_event = *event; 318 new_event.u.k.key = event->bytes[0]; 319 new_event.bytes[1] = 0xff; 320 #if NAED > 0 321 if (adb_polling || !aed_input(&new_event)) 322 #endif 323 #if NWSKBD > 0 324 kbd_intr(&new_event); 325 #else 326 /* do nothing */ ; 327 #endif 328 if (event->bytes[1] != 0xff) { 329 new_event.u.k.key = event->bytes[1]; 330 new_event.bytes[0] = event->bytes[1]; 331 new_event.bytes[1] = 0xff; 332 #if NAED > 0 333 if (adb_polling || !aed_input(&new_event)) 334 #endif 335 #if NWSKBD > 0 336 kbd_intr(&new_event); 337 #else 338 /* do nothing */ ; 339 #endif 340 } 341 342 } 343 344 #ifdef notyet 345 /* 346 * Get the actual hardware LED state and convert it to softc format. 347 */ 348 static u_char 349 getleds(addr) 350 int addr; 351 { 352 short cmd; 353 u_char buffer[9], leds; 354 355 leds = 0x00; /* all off */ 356 buffer[0] = 0; 357 kbd_done = 0; 358 359 cmd = ADBTALK(addr, 2); 360 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd); 361 while (!kbd_done) 362 /* busy-wait until done */ ; 363 364 if (buffer[0] > 0) 365 leds = ~(buffer[2]) & 0x07; 366 367 return (leds); 368 } 369 370 /* 371 * Set the keyboard LED's. 372 * 373 * Automatically translates from ioctl/softc format to the 374 * actual keyboard register format 375 */ 376 static int 377 setleds(ksc, leds) 378 struct akbd_softc *ksc; 379 u_char leds; 380 { 381 int addr; 382 short cmd; 383 u_char buffer[9]; 384 385 if ((leds & 0x07) == (ksc->sc_leds & 0x07)) 386 return (0); 387 388 addr = ksc->adbaddr; 389 buffer[0] = 0; 390 kbd_done = 0; 391 392 cmd = ADBTALK(addr, 2); 393 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd); 394 while (!kbd_done) 395 /* busy-wait until done */ ; 396 397 if (buffer[0] == 0) 398 return (EIO); 399 400 leds = ~leds & 0x07; 401 buffer[2] &= 0xf8; 402 buffer[2] |= leds; 403 404 cmd = ADBLISTEN(addr, 2); 405 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd); 406 while (!kbd_done) 407 /* busy-wait until done */ ; 408 409 /* talk R2 */ 410 cmd = ADBTALK(addr, 2); 411 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd); 412 while (!kbd_done) 413 /* busy-wait until done */ ; 414 415 if (buffer[0] == 0) 416 return (EIO); 417 418 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07; 419 420 if ((buffer[2] & 0xf8) != leds) 421 return (EIO); 422 else 423 return (0); 424 } 425 426 /* 427 * Toggle all of the LED's on and off, just for show. 428 */ 429 static void 430 blinkleds(ksc) 431 struct akbd_softc *ksc; 432 { 433 int addr, i; 434 u_char blinkleds, origleds; 435 436 addr = ksc->adbaddr; 437 origleds = getleds(addr); 438 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK; 439 440 (void)setleds(ksc, blinkleds); 441 442 for (i = 0; i < 10000; i++) 443 delay(50); 444 445 /* make sure that we restore the LED settings */ 446 i = 10; 447 do { 448 (void)setleds(ksc, (u_char)0x00); 449 } while (setleds(ksc, (u_char)0x00) && (i-- > 0)); 450 451 return; 452 } 453 #endif 454 455 int 456 akbd_is_console() 457 { 458 extern struct mac68k_machine_S mac68k_machine; 459 460 return ((mac68k_machine.serial_console & 0x03) == 0); 461 } 462 463 int 464 akbd_enable(v, on) 465 void *v; 466 int on; 467 { 468 return 0; 469 } 470 471 void 472 akbd_set_leds(v, on) 473 void *v; 474 int on; 475 { 476 } 477 478 int 479 akbd_ioctl(v, cmd, data, flag, p) 480 void *v; 481 u_long cmd; 482 caddr_t data; 483 int flag; 484 struct proc *p; 485 { 486 switch (cmd) { 487 488 case WSKBDIO_GTYPE: 489 *(int *)data = 0; /* XXX */ 490 return 0; 491 case WSKBDIO_SETLEDS: 492 return 0; 493 case WSKBDIO_GETLEDS: 494 *(int *)data = 0; 495 return 0; 496 case WSKBDIO_BELL: 497 case WSKBDIO_COMPLEXBELL: 498 #define d ((struct wskbd_bell_data *)data) 499 mac68k_ring_bell(d->pitch, d->period * HZ / 1000, 100); 500 /* comes in as msec, goes out as ticks; volume ignored */ 501 #undef d 502 return (0); 503 } 504 /* kbdioctl(...); */ 505 506 return -1; 507 } 508 509 static int polledkey; 510 extern int adb_polling; 511 512 int 513 kbd_intr(event) 514 adb_event_t *event; 515 { 516 int key, press, val; 517 int type; 518 519 struct akbd_softc *sc = akbd_cd.cd_devs[0]; 520 521 key = event->u.k.key; 522 press = ADBK_PRESS(key); 523 val = ADBK_KEYVAL(key); 524 525 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 526 527 if (key == 185) { /* Caps Lock released */ 528 type = WSCONS_EVENT_KEY_DOWN; 529 wskbd_input(sc->sc_wskbddev, type, val); 530 type = WSCONS_EVENT_KEY_UP; 531 } 532 533 if (adb_polling) 534 polledkey = key; 535 else 536 wskbd_input(sc->sc_wskbddev, type, val); 537 538 return 0; 539 } 540 541 int 542 akbd_cnattach() 543 { 544 if (!akbd_is_console()) 545 return -1; 546 547 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata); 548 549 return 0; 550 } 551 552 void 553 akbd_cngetc(v, type, data) 554 void *v; 555 u_int *type; 556 int *data; 557 { 558 int intbits, key, press, val; 559 int s; 560 561 s = splhigh(); 562 563 polledkey = -1; 564 adb_polling = 1; 565 566 while (polledkey == -1) { 567 intbits = via_reg(VIA1, vIFR); 568 569 if (intbits & V1IF_ADBRDY) { 570 mrg_adbintr(); 571 via_reg(VIA1, vIFR) = V1IF_ADBRDY; 572 } 573 if (intbits & 0x10) { 574 mrg_pmintr(); 575 via_reg(VIA1, vIFR) = 0x10; 576 } 577 } 578 579 adb_polling = 0; 580 splx(s); 581 582 key = polledkey; 583 press = ADBK_PRESS(key); 584 val = ADBK_KEYVAL(key); 585 586 *data = val; 587 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 588 } 589 590 void 591 akbd_cnpollc(v, on) 592 void *v; 593 int on; 594 { 595 } 596