1 /* $NetBSD: akbd.c,v 1.13 2002/03/17 19:40:43 atatat 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 #include <sys/kernel.h> 44 45 #include "aed.h" 46 #include "wskbd.h" 47 48 #include <dev/wscons/wsconsio.h> 49 #include <dev/wscons/wskbdvar.h> 50 #include <dev/wscons/wsksymdef.h> 51 #include <dev/wscons/wsksymvar.h> 52 53 #include <machine/autoconf.h> 54 #include <machine/cpu.h> 55 #define KEYBOARD_ARRAY 56 #include <machine/keyboard.h> 57 #include <machine/viareg.h> 58 59 #include <mac68k/mac68k/macrom.h> 60 #include <mac68k/dev/adbvar.h> 61 #include <mac68k/dev/aedvar.h> 62 #include <mac68k/dev/akbdmap.h> 63 #include <mac68k/dev/akbdvar.h> 64 #include <mac68k/dev/amsvar.h> 65 66 /* 67 * Function declarations. 68 */ 69 static int akbdmatch __P((struct device *, struct cfdata *, void *)); 70 static void akbdattach __P((struct device *, struct device *, void *)); 71 void kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command)); 72 static void kbd_processevent __P((adb_event_t *event, struct akbd_softc *)); 73 #ifdef notyet 74 static u_char getleds __P((int)); 75 static int setleds __P((struct akbd_softc *, u_char)); 76 static void blinkleds __P((struct akbd_softc *)); 77 #endif 78 79 /* 80 * Local variables. 81 */ 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, struct akbd_softc *)); 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 error, kbd_done; 140 short cmd; 141 u_char buffer[9]; 142 #if NWSKBD > 0 143 struct wskbddev_attach_args a; 144 static int akbd_console_initted = 0; 145 int wskbd_eligible; 146 147 wskbd_eligible = 1; 148 #endif 149 150 sc->origaddr = aa_args->origaddr; 151 sc->adbaddr = aa_args->adbaddr; 152 sc->handler_id = aa_args->handler_id; 153 154 sc->sc_leds = (u_int8_t)0x00; /* initially off */ 155 156 adbinfo.siServiceRtPtr = (Ptr)adb_kbd_asmcomplete; 157 adbinfo.siDataAreaAddr = (caddr_t)sc; 158 159 switch (sc->handler_id) { 160 case ADB_STDKBD: 161 printf("standard keyboard\n"); 162 break; 163 case ADB_ISOKBD: 164 printf("standard keyboard (ISO layout)\n"); 165 break; 166 case ADB_EXTKBD: 167 cmd = ADBTALK(sc->adbaddr, 1); 168 kbd_done = 169 (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0); 170 171 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */ 172 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) { 173 printf("Mouseman (non-EMP) pseudo keyboard\n"); 174 adbinfo.siServiceRtPtr = (Ptr)0; 175 adbinfo.siDataAreaAddr = (Ptr)0; 176 #if NWSKBD > 0 177 wskbd_eligible = 0; 178 #endif /* NWSKBD > 0 */ 179 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) { 180 printf("Trackman (non-EMP) pseudo keyboard\n"); 181 adbinfo.siServiceRtPtr = (Ptr)0; 182 adbinfo.siDataAreaAddr = (Ptr)0; 183 #if NWSKBD > 0 184 wskbd_eligible = 0; 185 #endif /* NWSKBD > 0 */ 186 } else { 187 printf("extended keyboard\n"); 188 #ifdef notyet 189 blinkleds(sc); 190 #endif 191 } 192 break; 193 case ADB_EXTISOKBD: 194 printf("extended keyboard (ISO layout)\n"); 195 #ifdef notyet 196 blinkleds(sc); 197 #endif 198 break; 199 case ADB_KBDII: 200 printf("keyboard II\n"); 201 break; 202 case ADB_ISOKBDII: 203 printf("keyboard II (ISO layout)\n"); 204 break; 205 case ADB_PBKBD: 206 printf("PowerBook keyboard\n"); 207 break; 208 case ADB_PBISOKBD: 209 printf("PowerBook keyboard (ISO layout)\n"); 210 break; 211 case ADB_ADJKPD: 212 printf("adjustable keypad\n"); 213 #if NWSKBD > 0 214 wskbd_eligible = 0; 215 #endif /* NWSKBD > 0 */ 216 break; 217 case ADB_ADJKBD: 218 printf("adjustable keyboard\n"); 219 break; 220 case ADB_ADJISOKBD: 221 printf("adjustable keyboard (ISO layout)\n"); 222 break; 223 case ADB_ADJJAPKBD: 224 printf("adjustable keyboard (Japanese layout)\n"); 225 break; 226 case ADB_PBEXTISOKBD: 227 printf("PowerBook extended keyboard (ISO layout)\n"); 228 break; 229 case ADB_PBEXTJAPKBD: 230 printf("PowerBook extended keyboard (Japanese layout)\n"); 231 break; 232 case ADB_JPKBDII: 233 printf("keyboard II (Japanese layout)\n"); 234 break; 235 case ADB_PBEXTKBD: 236 printf("PowerBook extended keyboard\n"); 237 break; 238 case ADB_DESIGNKBD: 239 printf("extended keyboard\n"); 240 #ifdef notyet 241 blinkleds(sc); 242 #endif 243 break; 244 case ADB_PBJPKBD: 245 printf("PowerBook keyboard (Japanese layout)\n"); 246 break; 247 default: 248 printf("mapped device (%d)\n", sc->handler_id); 249 #if NWSKBD > 0 250 wskbd_eligible = 0; 251 #endif /* NWSKBD > 0 */ 252 break; 253 } 254 error = SetADBInfo(&adbinfo, sc->adbaddr); 255 #ifdef ADB_DEBUG 256 if (adb_debug) 257 printf("akbd: returned %d from SetADBInfo\n", error); 258 #endif 259 260 #if NWSKBD > 0 261 if (akbd_is_console() && wskbd_eligible) 262 a.console = (++akbd_console_initted == 1); 263 else 264 a.console = 0; 265 a.keymap = &akbd_keymapdata; 266 a.accessops = &akbd_accessops; 267 a.accesscookie = sc; 268 269 sc->sc_wskbddev = config_found(self, &a, wskbddevprint); 270 #endif 271 } 272 273 274 /* 275 * Handle putting the keyboard data received from the ADB into 276 * an ADB event record. 277 */ 278 void 279 kbd_adbcomplete(buffer, data_area, adb_command) 280 caddr_t buffer; 281 caddr_t data_area; 282 int adb_command; 283 { 284 adb_event_t event; 285 struct akbd_softc *ksc; 286 int adbaddr; 287 #ifdef ADB_DEBUG 288 int i; 289 290 if (adb_debug) 291 printf("adb: transaction completion\n"); 292 #endif 293 294 adbaddr = ADB_CMDADDR(adb_command); 295 ksc = (struct akbd_softc *)data_area; 296 297 event.addr = adbaddr; 298 event.hand_id = ksc->handler_id; 299 event.def_addr = ksc->origaddr; 300 event.byte_count = buffer[0]; 301 memcpy(event.bytes, buffer + 1, event.byte_count); 302 303 #ifdef ADB_DEBUG 304 if (adb_debug) { 305 printf("akbd: from %d at %d (org %d) %d:", event.addr, 306 event.hand_id, event.def_addr, buffer[0]); 307 for (i = 1; i <= buffer[0]; i++) 308 printf(" %x", buffer[i]); 309 printf("\n"); 310 } 311 #endif 312 313 microtime(&event.timestamp); 314 315 kbd_processevent(&event, ksc); 316 } 317 318 /* 319 * Given a keyboard ADB event, record the keycodes and call the key 320 * repeat handler, optionally passing the event through the mouse 321 * button emulation handler first. 322 */ 323 static void 324 kbd_processevent(event, ksc) 325 adb_event_t *event; 326 struct akbd_softc *ksc; 327 { 328 adb_event_t new_event; 329 330 new_event = *event; 331 new_event.u.k.key = event->bytes[0]; 332 new_event.bytes[1] = 0xff; 333 #if NAED > 0 334 if (adb_polling || !aed_input(&new_event)) 335 #endif 336 #if NWSKBD > 0 337 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 338 kbd_intr(&new_event, ksc); 339 #else 340 /* do nothing */ ; 341 #endif 342 if (event->bytes[1] != 0xff) { 343 new_event.u.k.key = event->bytes[1]; 344 new_event.bytes[0] = event->bytes[1]; 345 new_event.bytes[1] = 0xff; 346 #if NAED > 0 347 if (adb_polling || !aed_input(&new_event)) 348 #endif 349 #if NWSKBD > 0 350 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */ 351 kbd_intr(&new_event, ksc); 352 #else 353 /* do nothing */ ; 354 #endif 355 } 356 357 } 358 359 #ifdef notyet 360 /* 361 * Get the actual hardware LED state and convert it to softc format. 362 */ 363 static u_char 364 getleds(addr) 365 int addr; 366 { 367 short cmd; 368 u_char buffer[9], leds; 369 370 leds = 0x00; /* all off */ 371 buffer[0] = 0; 372 373 cmd = ADBTALK(addr, 2); 374 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 && 375 buffer[0] > 0) 376 leds = ~(buffer[2]) & 0x07; 377 378 return (leds); 379 } 380 381 /* 382 * Set the keyboard LED's. 383 * 384 * Automatically translates from ioctl/softc format to the 385 * actual keyboard register format 386 */ 387 static int 388 setleds(ksc, leds) 389 struct akbd_softc *ksc; 390 u_char leds; 391 { 392 int addr; 393 short cmd; 394 u_char buffer[9]; 395 396 if ((leds & 0x07) == (ksc->sc_leds & 0x07)) 397 return (0); 398 399 addr = ksc->adbaddr; 400 buffer[0] = 0; 401 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 leds = ~leds & 0x07; 407 buffer[2] &= 0xf8; 408 buffer[2] |= leds; 409 410 cmd = ADBLISTEN(addr, 2); 411 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 412 413 /* talk R2 */ 414 cmd = ADBTALK(addr, 2); 415 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || 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 EPASSTHROUGH; 507 } 508 509 static int polledkey; 510 extern int adb_polling; 511 512 int 513 kbd_intr(event, sc) 514 adb_event_t *event; 515 struct akbd_softc *sc; 516 { 517 int key, press, val; 518 int type; 519 520 key = event->u.k.key; 521 press = ADBK_PRESS(key); 522 val = ADBK_KEYVAL(key); 523 524 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 525 526 if (key == 185) { /* Caps Lock released */ 527 type = WSCONS_EVENT_KEY_DOWN; 528 wskbd_input(sc->sc_wskbddev, type, val); 529 type = WSCONS_EVENT_KEY_UP; 530 } 531 532 if (adb_polling) 533 polledkey = key; 534 else 535 wskbd_input(sc->sc_wskbddev, type, val); 536 537 return 0; 538 } 539 540 int 541 akbd_cnattach() 542 { 543 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata); 544 545 return 0; 546 } 547 548 void 549 akbd_cngetc(v, type, data) 550 void *v; 551 u_int *type; 552 int *data; 553 { 554 int intbits, key, press, val; 555 int s; 556 557 s = splhigh(); 558 559 polledkey = -1; 560 adb_polling = 1; 561 562 while (polledkey == -1) { 563 intbits = via_reg(VIA1, vIFR); 564 565 if (intbits & V1IF_ADBRDY) { 566 mrg_adbintr(); 567 via_reg(VIA1, vIFR) = V1IF_ADBRDY; 568 } 569 if (intbits & 0x10) { 570 mrg_pmintr(); 571 via_reg(VIA1, vIFR) = 0x10; 572 } 573 } 574 575 adb_polling = 0; 576 splx(s); 577 578 key = polledkey; 579 press = ADBK_PRESS(key); 580 val = ADBK_KEYVAL(key); 581 582 *data = val; 583 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP; 584 } 585 586 void 587 akbd_cnpollc(v, on) 588 void *v; 589 int on; 590 { 591 } 592