1 /* $NetBSD: kbd.c,v 1.19 1997/11/03 20:18:00 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)kbd.c 8.2 (Berkeley) 10/30/93 45 */ 46 47 /* 48 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers 49 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and 50 * passes them up to the appropriate reader. 51 */ 52 53 /* 54 * Zilog Z8530 Dual UART driver (keyboard interface) 55 * 56 * This is the "slave" driver that will be attached to 57 * the "zsc" driver for a Sun keyboard. 58 */ 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/conf.h> 63 #include <sys/device.h> 64 #include <sys/ioctl.h> 65 #include <sys/kernel.h> 66 #include <sys/proc.h> 67 #include <sys/signal.h> 68 #include <sys/signalvar.h> 69 #include <sys/time.h> 70 #include <sys/syslog.h> 71 #include <sys/select.h> 72 #include <sys/poll.h> 73 74 #include <dev/ic/z8530reg.h> 75 #include <machine/z8530var.h> 76 #include <machine/vuid_event.h> 77 #include <machine/kbd.h> 78 #include <machine/kbio.h> 79 80 #include "event_var.h" 81 #include "kbd_xlate.h" 82 #include "locators.h" 83 84 /* 85 * Ideas: 86 * /dev/kbd is not a tty (plain device) 87 */ 88 89 /* 90 * How many input characters we can buffer. 91 * The port-specific var.h may override this. 92 * Note: must be a power of two! 93 */ 94 #define KBD_RX_RING_SIZE 256 95 #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1) 96 /* 97 * Output buffer. Only need a few chars. 98 */ 99 #define KBD_TX_RING_SIZE 16 100 #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1) 101 /* 102 * Keyboard serial line speed is fixed at 1200 bps. 103 */ 104 #define KBD_BPS 1200 105 #define KBD_RESET_TIMO 1000 /* mS. */ 106 107 /* 108 * XXX - Historical comment - no longer quite right... 109 * Keyboard driver state. The ascii and kbd links go up and down and 110 * we just sit in the middle doing translation. Note that it is possible 111 * to get just one of the two links, in which case /dev/kbd is unavailable. 112 * The downlink supplies us with `internal' open and close routines which 113 * will enable dataflow across the downlink. We promise to call open when 114 * we are willing to take keystrokes, and to call close when we are not. 115 * If /dev/kbd is not the console tty input source, we do this whenever 116 * /dev/kbd is in use; otherwise we just leave it open forever. 117 */ 118 struct kbd_softc { 119 struct device k_dev; /* required first: base device */ 120 struct zs_chanstate *k_cs; 121 122 /* Flags to communicate with kbd_softint() */ 123 volatile int k_intr_flags; 124 #define INTR_RX_OVERRUN 1 125 #define INTR_TX_EMPTY 2 126 #define INTR_ST_CHECK 4 127 128 /* Transmit state */ 129 volatile int k_txflags; 130 #define K_TXBUSY 1 131 #define K_TXWANT 2 132 133 /* 134 * State of upper interface. 135 */ 136 int k_isopen; /* set if open has been done */ 137 int k_evmode; /* set if we should produce events */ 138 struct evvar k_events; /* event queue state */ 139 140 /* 141 * ACSI translation state 142 */ 143 int k_repeat_start; /* initial delay */ 144 int k_repeat_step; /* inter-char delay */ 145 int k_repeatsym; /* repeating symbol */ 146 int k_repeating; /* we've called timeout() */ 147 struct kbd_state k_state; /* ASCII translation state */ 148 149 /* 150 * Magic sequence stuff (L1-A) 151 */ 152 char k_isconsole; 153 char k_magic1_down; 154 u_char k_magic1; /* L1 */ 155 u_char k_magic2; /* A */ 156 157 /* 158 * The transmit ring buffer. 159 */ 160 volatile u_int k_tbget; /* transmit buffer `get' index */ 161 volatile u_int k_tbput; /* transmit buffer `put' index */ 162 u_char k_tbuf[KBD_TX_RING_SIZE]; /* data */ 163 164 /* 165 * The receive ring buffer. 166 */ 167 u_int k_rbget; /* ring buffer `get' index */ 168 volatile u_int k_rbput; /* ring buffer `put' index */ 169 u_short k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */ 170 171 }; 172 173 /* Prototypes */ 174 static void kbd_new_layout(struct kbd_softc *k); 175 static void kbd_output(struct kbd_softc *k, int c); 176 static void kbd_repeat(void *arg); 177 static void kbd_set_leds(struct kbd_softc *k, int leds); 178 static void kbd_start_tx(struct kbd_softc *k); 179 static void kbd_update_leds(struct kbd_softc *k); 180 static void kbd_was_reset(struct kbd_softc *k); 181 static int kbd_drain_tx(struct kbd_softc *k); 182 183 cdev_decl(kbd); /* open, close, read, write, ioctl, stop, ... */ 184 185 struct zsops zsops_kbd; 186 187 /**************************************************************** 188 * Definition of the driver for autoconfig. 189 ****************************************************************/ 190 191 static int kbd_match(struct device *, struct cfdata *, void *); 192 static void kbd_attach(struct device *, struct device *, void *); 193 194 struct cfattach kbd_ca = { 195 sizeof(struct kbd_softc), kbd_match, kbd_attach 196 }; 197 198 struct cfdriver kbd_cd = { 199 NULL, "kbd", DV_DULL 200 }; 201 202 203 /* 204 * kbd_match: how is this zs channel configured? 205 */ 206 int 207 kbd_match(parent, cf, aux) 208 struct device *parent; 209 struct cfdata *cf; 210 void *aux; 211 { 212 struct zsc_attach_args *args = aux; 213 214 /* Exact match required for keyboard. */ 215 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel) 216 return 2; 217 218 return 0; 219 } 220 221 void 222 kbd_attach(parent, self, aux) 223 struct device *parent, *self; 224 void *aux; 225 226 { 227 struct zsc_softc *zsc = (void *) parent; 228 struct kbd_softc *k = (void *) self; 229 struct zsc_attach_args *args = aux; 230 struct zs_chanstate *cs; 231 struct cfdata *cf; 232 int channel, kbd_unit; 233 int reset, s; 234 235 cf = k->k_dev.dv_cfdata; 236 kbd_unit = k->k_dev.dv_unit; 237 channel = args->channel; 238 cs = zsc->zsc_cs[channel]; 239 cs->cs_private = k; 240 cs->cs_ops = &zsops_kbd; 241 k->k_cs = cs; 242 243 if (args->hwflags & ZS_HWFLAG_CONSOLE) { 244 k->k_isconsole = 1; 245 printf(" (console)"); 246 } 247 printf("\n"); 248 249 /* Initialize the speed, etc. */ 250 s = splzs(); 251 if (k->k_isconsole == 0) { 252 /* Not the console; may need reset. */ 253 reset = (channel == 0) ? 254 ZSWR9_A_RESET : ZSWR9_B_RESET; 255 zs_write_reg(cs, 9, reset); 256 } 257 /* These are OK as set by zscc: WR3, WR4, WR5 */ 258 /* We don't care about status interrupts. */ 259 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE; 260 (void) zs_set_speed(cs, KBD_BPS); 261 zs_loadchannelregs(cs); 262 splx(s); 263 264 /* Do this before any calls to kbd_rint(). */ 265 kbd_xlate_init(&k->k_state); 266 267 /* XXX - Do this in open? */ 268 k->k_repeat_start = hz/2; 269 k->k_repeat_step = hz/20; 270 271 /* Magic sequence. */ 272 k->k_magic1 = KBD_L1; 273 k->k_magic2 = KBD_A; 274 275 /* Now attach the (kd) pseudo-driver. */ 276 kd_init(kbd_unit); 277 } 278 279 280 /**************************************************************** 281 * Entry points for /dev/kbd 282 * (open,close,read,write,...) 283 ****************************************************************/ 284 285 /* 286 * Open: 287 * Check exclusion, open actual device (_iopen), 288 * setup event channel, clear ASCII repeat stuff. 289 */ 290 int 291 kbdopen(dev, flags, mode, p) 292 dev_t dev; 293 int flags, mode; 294 struct proc *p; 295 { 296 struct kbd_softc *k; 297 int error, unit; 298 299 unit = minor(dev); 300 if (unit >= kbd_cd.cd_ndevs) 301 return (ENXIO); 302 k = kbd_cd.cd_devs[unit]; 303 if (k == NULL) 304 return (ENXIO); 305 306 /* Exclusive open required for /dev/kbd */ 307 if (k->k_events.ev_io) 308 return (EBUSY); 309 k->k_events.ev_io = p; 310 311 if ((error = kbd_iopen(unit)) != 0) { 312 k->k_events.ev_io = NULL; 313 return (error); 314 } 315 ev_init(&k->k_events); 316 k->k_evmode = 1; /* XXX: OK? */ 317 318 if (k->k_repeating) { 319 k->k_repeating = 0; 320 untimeout(kbd_repeat, k); 321 } 322 323 return (0); 324 } 325 326 /* 327 * Close: 328 * Turn off event mode, dump the queue, and close the keyboard 329 * unless it is supplying console input. 330 */ 331 int 332 kbdclose(dev, flags, mode, p) 333 dev_t dev; 334 int flags, mode; 335 struct proc *p; 336 { 337 struct kbd_softc *k; 338 339 k = kbd_cd.cd_devs[minor(dev)]; 340 k->k_evmode = 0; 341 ev_fini(&k->k_events); 342 k->k_events.ev_io = NULL; 343 return (0); 344 } 345 346 int 347 kbdread(dev, uio, flags) 348 dev_t dev; 349 struct uio *uio; 350 int flags; 351 { 352 struct kbd_softc *k; 353 354 k = kbd_cd.cd_devs[minor(dev)]; 355 return (ev_read(&k->k_events, uio, flags)); 356 } 357 358 /* this routine should not exist, but is convenient to write here for now */ 359 int 360 kbdwrite(dev, uio, flags) 361 dev_t dev; 362 struct uio *uio; 363 int flags; 364 { 365 366 return (EOPNOTSUPP); 367 } 368 369 int 370 kbdpoll(dev, events, p) 371 dev_t dev; 372 int events; 373 struct proc *p; 374 { 375 struct kbd_softc *k; 376 377 k = kbd_cd.cd_devs[minor(dev)]; 378 return (ev_poll(&k->k_events, events, p)); 379 } 380 381 382 static int kbd_iockeymap __P((struct kbd_state *ks, 383 u_long cmd, struct kiockeymap *kio)); 384 385 static int kbd_iocsled(struct kbd_softc *k, char *data); 386 387 #ifdef KIOCGETKEY 388 static int kbd_oldkeymap __P((struct kbd_state *ks, 389 u_long cmd, struct okiockey *okio)); 390 #endif 391 392 int 393 kbdioctl(dev, cmd, data, flag, p) 394 dev_t dev; 395 u_long cmd; 396 register caddr_t data; 397 int flag; 398 struct proc *p; 399 { 400 struct kbd_softc *k; 401 struct kbd_state *ks; 402 int error = 0; 403 404 k = kbd_cd.cd_devs[minor(dev)]; 405 ks = &k->k_state; 406 407 switch (cmd) { 408 409 case KIOCTRANS: /* Set translation mode */ 410 /* We only support "raw" mode on /dev/kbd */ 411 if (*(int *)data != TR_UNTRANS_EVENT) 412 error = EINVAL; 413 break; 414 415 case KIOCGTRANS: /* Get translation mode */ 416 /* We only support "raw" mode on /dev/kbd */ 417 *(int *)data = TR_UNTRANS_EVENT; 418 break; 419 420 #ifdef KIOCGETKEY 421 case KIOCGETKEY: /* Get keymap entry (old format) */ 422 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data); 423 break; 424 #endif KIOCGETKEY */ 425 426 case KIOCSKEY: /* Set keymap entry */ 427 /* fallthrough */ 428 case KIOCGKEY: /* Get keymap entry */ 429 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data); 430 break; 431 432 case KIOCCMD: /* Send a command to the keyboard */ 433 error = kbd_docmd(*(int *)data, 1); 434 break; 435 436 case KIOCTYPE: /* Get keyboard type */ 437 *(int *)data = ks->kbd_id; 438 break; 439 440 case KIOCSDIRECT: /* where to send input */ 441 k->k_evmode = *(int *)data; 442 break; 443 444 case KIOCLAYOUT: /* Get keyboard layout */ 445 *(int *)data = ks->kbd_layout; 446 break; 447 448 case KIOCSLED: 449 error = kbd_iocsled(k, (char *)data); 450 break; 451 452 case KIOCGLED: 453 *(char *)data = ks->kbd_leds; 454 break; 455 456 case FIONBIO: /* we will remove this someday (soon???) */ 457 break; 458 459 case FIOASYNC: 460 k->k_events.ev_async = *(int *)data != 0; 461 break; 462 463 case TIOCSPGRP: 464 if (*(int *)data != k->k_events.ev_io->p_pgid) 465 error = EPERM; 466 break; 467 468 default: 469 error = ENOTTY; 470 break; 471 } 472 473 return (error); 474 } 475 476 /**************************************************************** 477 * ioctl helpers 478 ****************************************************************/ 479 480 /* 481 * Get/Set keymap entry 482 */ 483 static int 484 kbd_iockeymap(ks, cmd, kio) 485 struct kbd_state *ks; 486 u_long cmd; 487 struct kiockeymap *kio; 488 { 489 u_short *km; 490 u_int station; 491 492 switch (kio->kio_tablemask) { 493 case KIOC_NOMASK: 494 km = ks->kbd_k.k_normal; 495 break; 496 case KIOC_SHIFTMASK: 497 km = ks->kbd_k.k_shifted; 498 break; 499 case KIOC_CTRLMASK: 500 km = ks->kbd_k.k_control; 501 break; 502 case KIOC_UPMASK: 503 km = ks->kbd_k.k_release; 504 break; 505 default: 506 /* Silently ignore unsupported masks */ 507 return (0); 508 } 509 510 /* Range-check the table position. */ 511 station = kio->kio_station; 512 if (station >= KEYMAP_SIZE) 513 return (EINVAL); 514 515 switch (cmd) { 516 517 case KIOCGKEY: /* Get keymap entry */ 518 kio->kio_entry = km[station]; 519 break; 520 521 case KIOCSKEY: /* Set keymap entry */ 522 km[station] = kio->kio_entry; 523 break; 524 525 default: 526 return(ENOTTY); 527 } 528 return (0); 529 } 530 531 #ifdef KIOCGETKEY 532 /* 533 * Get/Set keymap entry, 534 * old format (compatibility) 535 */ 536 int 537 kbd_oldkeymap(ks, cmd, kio) 538 struct kbd_state *ks; 539 u_long cmd; 540 struct okiockey *kio; 541 { 542 int error = 0; 543 544 switch (cmd) { 545 546 case KIOCGETKEY: 547 if (kio->kio_station == 118) { 548 /* 549 * This is X11 asking if a type 3 keyboard is 550 * really a type 3 keyboard. Say yes, it is, 551 * by reporting key station 118 as a "hole". 552 * Note old (SunOS 3.5) definition of HOLE! 553 */ 554 kio->kio_entry = 0xA2; 555 break; 556 } 557 /* fall through */ 558 559 default: 560 error = ENOTTY; 561 break; 562 } 563 564 return (error); 565 } 566 #endif /* KIOCGETKEY */ 567 568 569 /* 570 * keyboard command ioctl 571 * ``unimplemented commands are ignored'' (blech) 572 * This is also export to the fb driver. 573 */ 574 int 575 kbd_docmd(cmd, isuser) 576 int cmd; 577 int isuser; 578 { 579 struct kbd_softc *k; 580 struct kbd_state *ks; 581 int error, s; 582 583 error = 0; 584 k = kbd_cd.cd_devs[0]; 585 ks = &k->k_state; 586 587 switch (cmd) { 588 589 case KBD_CMD_BELL: 590 case KBD_CMD_NOBELL: 591 /* Supported by type 2, 3, and 4 keyboards */ 592 break; 593 594 case KBD_CMD_CLICK: 595 case KBD_CMD_NOCLICK: 596 /* Unsupported by type 2 keyboards */ 597 if (ks->kbd_id <= KB_SUN2) 598 return (0); 599 ks->kbd_click = (cmd == KBD_CMD_CLICK); 600 break; 601 602 default: 603 return (0); 604 } 605 606 s = spltty(); 607 608 if (isuser) 609 error = kbd_drain_tx(k); 610 611 if (error == 0) { 612 kbd_output(k, cmd); 613 kbd_start_tx(k); 614 } 615 616 splx(s); 617 618 return (error); 619 } 620 621 /* 622 * Set LEDs ioctl. 623 */ 624 static int 625 kbd_iocsled(k, data) 626 struct kbd_softc *k; 627 char *data; 628 { 629 int leds, error, s; 630 631 leds = *data; 632 633 s = spltty(); 634 error = kbd_drain_tx(k); 635 if (error == 0) { 636 kbd_set_leds(k, leds); 637 } 638 splx(s); 639 640 return (error); 641 } 642 643 644 /**************************************************************** 645 * middle layers: 646 * - keysym to ASCII sequence 647 * - raw key codes to keysym 648 ****************************************************************/ 649 650 static void kbd_input_string __P((struct kbd_softc *, char *)); 651 static void kbd_input_funckey __P((struct kbd_softc *, int)); 652 static int kbd_input_keysym __P((struct kbd_softc *, int)); 653 static void kbd_input_raw __P((struct kbd_softc *k, int)); 654 655 /* 656 * Initialization done by either kdcninit or kbd_iopen 657 */ 658 void 659 kbd_xlate_init(ks) 660 struct kbd_state *ks; 661 { 662 struct keyboard *ktbls; 663 int id; 664 665 id = ks->kbd_id; 666 if (id < KBD_MIN_TYPE) 667 id = KBD_MIN_TYPE; 668 if (id > kbd_max_type) 669 id = kbd_max_type; 670 ktbls = keyboards[id]; 671 672 ks->kbd_k = *ktbls; /* struct assignment */ 673 ks->kbd_modbits = 0; 674 } 675 676 /* 677 * Turn keyboard up/down codes into a KEYSYM. 678 * Note that the "kd" driver uses this too! 679 */ 680 int 681 kbd_code_to_keysym(ks, c) 682 register struct kbd_state *ks; 683 register int c; 684 { 685 u_short *km; 686 int keysym; 687 688 /* 689 * Get keymap pointer. One of these: 690 * release, control, shifted, normal, ... 691 */ 692 if (KEY_UP(c)) 693 km = ks->kbd_k.k_release; 694 else if (ks->kbd_modbits & KBMOD_CTRL_MASK) 695 km = ks->kbd_k.k_control; 696 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK) 697 km = ks->kbd_k.k_shifted; 698 else 699 km = ks->kbd_k.k_normal; 700 701 if (km == NULL) { 702 /* 703 * Do not know how to translate yet. 704 * We will find out when a RESET comes along. 705 */ 706 return (KEYSYM_NOP); 707 } 708 keysym = km[KEY_CODE(c)]; 709 710 /* 711 * Post-processing for Caps-lock 712 */ 713 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) && 714 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) ) 715 { 716 if (('a' <= keysym) && (keysym <= 'z')) 717 keysym -= ('a' - 'A'); 718 } 719 720 /* 721 * Post-processing for Num-lock. All "function" 722 * keysyms get indirected through another table. 723 * (XXX: Only if numlock on. Want off also!) 724 */ 725 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) && 726 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) ) 727 { 728 keysym = kbd_numlock_map[keysym & 0x3F]; 729 } 730 731 return (keysym); 732 } 733 734 void 735 kbd_input_string(k, str) 736 struct kbd_softc *k; 737 char *str; 738 { 739 while (*str) { 740 kd_input(*str); 741 str++; 742 } 743 } 744 745 void 746 kbd_input_funckey(k, keysym) 747 struct kbd_softc *k; 748 register int keysym; 749 { 750 register int n; 751 char str[12]; 752 753 /* 754 * Format the F-key sequence and send as a string. 755 * XXX: Ugly compatibility mappings. 756 */ 757 n = 0xC0 + (keysym & 0x3F); 758 sprintf(str, "\033[%dz", n); 759 kbd_input_string(k, str); 760 } 761 762 /* 763 * This is called by kbd_input_raw() or by kb_repeat() 764 * to deliver ASCII input. Called at spltty(). 765 * 766 * Return zero on success, else the keysym that we 767 * could not handle (so the caller may complain). 768 */ 769 int 770 kbd_input_keysym(k, keysym) 771 struct kbd_softc *k; 772 register int keysym; 773 { 774 struct kbd_state *ks = &k->k_state; 775 register int data; 776 777 switch (KEYSYM_CLASS(keysym)) { 778 779 case KEYSYM_ASCII: 780 data = KEYSYM_DATA(keysym); 781 if (ks->kbd_modbits & KBMOD_META_MASK) 782 data |= 0x80; 783 kd_input(data); 784 break; 785 786 case KEYSYM_STRING: 787 data = keysym & 0xF; 788 kbd_input_string(k, kbd_stringtab[data]); 789 break; 790 791 case KEYSYM_FUNC: 792 kbd_input_funckey(k, keysym); 793 break; 794 795 case KEYSYM_CLRMOD: 796 data = 1 << (keysym & 0x1F); 797 ks->kbd_modbits &= ~data; 798 break; 799 800 case KEYSYM_SETMOD: 801 data = 1 << (keysym & 0x1F); 802 ks->kbd_modbits |= data; 803 break; 804 805 case KEYSYM_INVMOD: 806 data = 1 << (keysym & 0x1F); 807 ks->kbd_modbits ^= data; 808 kbd_update_leds(k); 809 break; 810 811 case KEYSYM_ALL_UP: 812 ks->kbd_modbits &= ~0xFFFF; 813 break; 814 815 case KEYSYM_SPECIAL: 816 if (keysym == KEYSYM_NOP) 817 break; 818 /* fall through */ 819 default: 820 /* We could not handle it. */ 821 return (keysym); 822 } 823 return (0); 824 } 825 826 /* 827 * This is the autorepeat timeout function. 828 * Called at splsoftclock(). 829 */ 830 static void 831 kbd_repeat(void *arg) 832 { 833 struct kbd_softc *k = (struct kbd_softc *)arg; 834 int s = spltty(); 835 836 if (k->k_repeating && k->k_repeatsym >= 0) { 837 (void)kbd_input_keysym(k, k->k_repeatsym); 838 timeout(kbd_repeat, k, k->k_repeat_step); 839 } 840 splx(s); 841 } 842 843 /* 844 * Called by our kbd_softint() routine on input, 845 * which passes the raw hardware scan codes. 846 * Called at spltty() 847 */ 848 void 849 kbd_input_raw(k, c) 850 struct kbd_softc *k; 851 register int c; 852 { 853 struct kbd_state *ks = &k->k_state; 854 struct firm_event *fe; 855 int put, keysym; 856 857 /* XXX - Input errors already handled. */ 858 859 /* Are we expecting special input? */ 860 if (ks->kbd_expect) { 861 if (ks->kbd_expect & KBD_EXPECT_IDCODE) { 862 /* We read a KBD_RESET last time. */ 863 ks->kbd_id = c; 864 kbd_was_reset(k); 865 } 866 if (ks->kbd_expect & KBD_EXPECT_LAYOUT) { 867 /* We read a KBD_LAYOUT last time. */ 868 ks->kbd_layout = c; 869 kbd_new_layout(k); 870 } 871 ks->kbd_expect = 0; 872 return; 873 } 874 875 /* Is this one of the "special" input codes? */ 876 if (KBD_SPECIAL(c)) { 877 switch (c) { 878 case KBD_RESET: 879 ks->kbd_expect |= KBD_EXPECT_IDCODE; 880 /* Fake an "all-up" to resync. translation. */ 881 c = KBD_IDLE; 882 break; 883 884 case KBD_LAYOUT: 885 ks->kbd_expect |= KBD_EXPECT_LAYOUT; 886 return; 887 888 case KBD_ERROR: 889 log(LOG_WARNING, "%s: received error indicator\n", 890 k->k_dev.dv_xname); 891 return; 892 893 case KBD_IDLE: 894 /* Let this go to the translator. */ 895 break; 896 } 897 } 898 899 /* 900 * If /dev/kbd is not connected in event mode, 901 * translate and send upstream (to console). 902 */ 903 if (!k->k_evmode) { 904 905 /* Any input stops auto-repeat (i.e. key release). */ 906 if (k->k_repeating) { 907 k->k_repeating = 0; 908 untimeout(kbd_repeat, k); 909 } 910 911 /* Translate this code to a keysym */ 912 keysym = kbd_code_to_keysym(ks, c); 913 914 /* Pass up to the next layer. */ 915 if (kbd_input_keysym(k, keysym)) { 916 log(LOG_WARNING, "%s: code=0x%x with mod=0x%x" 917 " produced unexpected keysym 0x%x\n", 918 k->k_dev.dv_xname, c, 919 ks->kbd_modbits, keysym); 920 /* No point in auto-repeat here. */ 921 return; 922 } 923 924 /* Does this symbol get auto-repeat? */ 925 if (KEYSYM_NOREPEAT(keysym)) 926 return; 927 928 /* Setup for auto-repeat after initial delay. */ 929 k->k_repeating = 1; 930 k->k_repeatsym = keysym; 931 timeout(kbd_repeat, k, k->k_repeat_start); 932 return; 933 } 934 935 /* 936 * IDLEs confuse the MIT X11R4 server badly, so we must drop them. 937 * This is bad as it means the server will not automatically resync 938 * on all-up IDLEs, but I did not drop them before, and the server 939 * goes crazy when it comes time to blank the screen.... 940 */ 941 if (c == KBD_IDLE) 942 return; 943 944 /* 945 * Keyboard is generating events. Turn this keystroke into an 946 * event and put it in the queue. If the queue is full, the 947 * keystroke is lost (sorry!). 948 */ 949 put = k->k_events.ev_put; 950 fe = &k->k_events.ev_q[put]; 951 put = (put + 1) % EV_QSIZE; 952 if (put == k->k_events.ev_get) { 953 log(LOG_WARNING, "%s: event queue overflow\n", 954 k->k_dev.dv_xname); /* ??? */ 955 return; 956 } 957 fe->id = KEY_CODE(c); 958 fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN; 959 fe->time = time; 960 k->k_events.ev_put = put; 961 EV_WAKEUP(&k->k_events); 962 } 963 964 /**************************************************************** 965 * Interface to the lower layer (zscc) 966 ****************************************************************/ 967 968 static void kbd_rxint __P((struct zs_chanstate *)); 969 static void kbd_txint __P((struct zs_chanstate *)); 970 static void kbd_stint __P((struct zs_chanstate *)); 971 static void kbd_softint __P((struct zs_chanstate *)); 972 973 static void 974 kbd_rxint(cs) 975 register struct zs_chanstate *cs; 976 { 977 register struct kbd_softc *k; 978 register int put, put_next; 979 register u_char c, rr1; 980 981 k = cs->cs_private; 982 put = k->k_rbput; 983 984 /* 985 * First read the status, because reading the received char 986 * destroys the status of this char. 987 */ 988 rr1 = zs_read_reg(cs, 1); 989 c = zs_read_data(cs); 990 991 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) { 992 /* Clear the receive error. */ 993 zs_write_csr(cs, ZSWR0_RESET_ERRORS); 994 } 995 996 /* 997 * Check NOW for a console abort sequence, so that we can 998 * abort even when interrupts are locking up the machine. 999 */ 1000 if (k->k_magic1_down) { 1001 /* The last keycode was "MAGIC1" down. */ 1002 k->k_magic1_down = 0; 1003 if (c == k->k_magic2) { 1004 /* Magic "L1-A" sequence; enter debugger. */ 1005 if (k->k_isconsole) { 1006 zs_abort(cs); 1007 /* Debugger done. Fake L1-up to finish it. */ 1008 c = k->k_magic1 | KBD_UP; 1009 } else { 1010 printf("kbd: magic sequence, but not console\n"); 1011 } 1012 } 1013 } 1014 if (c == k->k_magic1) { 1015 k->k_magic1_down = 1; 1016 } 1017 1018 k->k_rbuf[put] = (c << 8) | rr1; 1019 put_next = (put + 1) & KBD_RX_RING_MASK; 1020 1021 /* Would overrun if increment makes (put==get). */ 1022 if (put_next == k->k_rbget) { 1023 k->k_intr_flags |= INTR_RX_OVERRUN; 1024 } else { 1025 /* OK, really increment. */ 1026 put = put_next; 1027 } 1028 1029 /* Done reading. */ 1030 k->k_rbput = put; 1031 1032 /* Ask for softint() call. */ 1033 cs->cs_softreq = 1; 1034 } 1035 1036 1037 static void 1038 kbd_txint(cs) 1039 register struct zs_chanstate *cs; 1040 { 1041 register struct kbd_softc *k; 1042 1043 k = cs->cs_private; 1044 zs_write_csr(cs, ZSWR0_RESET_TXINT); 1045 k->k_intr_flags |= INTR_TX_EMPTY; 1046 /* Ask for softint() call. */ 1047 cs->cs_softreq = 1; 1048 } 1049 1050 1051 static void 1052 kbd_stint(cs) 1053 register struct zs_chanstate *cs; 1054 { 1055 register struct kbd_softc *k; 1056 register int rr0; 1057 1058 k = cs->cs_private; 1059 1060 rr0 = zs_read_csr(cs); 1061 zs_write_csr(cs, ZSWR0_RESET_STATUS); 1062 1063 #if 0 1064 if (rr0 & ZSRR0_BREAK) { 1065 /* Keyboard unplugged? */ 1066 zs_abort(cs); 1067 return (0); 1068 } 1069 #endif 1070 1071 /* 1072 * We have to accumulate status line changes here. 1073 * Otherwise, if we get multiple status interrupts 1074 * before the softint runs, we could fail to notice 1075 * some status line changes in the softint routine. 1076 * Fix from Bill Studenmund, October 1996. 1077 */ 1078 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0); 1079 cs->cs_rr0 = rr0; 1080 k->k_intr_flags |= INTR_ST_CHECK; 1081 1082 /* Ask for softint() call. */ 1083 cs->cs_softreq = 1; 1084 } 1085 1086 /* 1087 * Get input from the recieve ring and pass it on. 1088 * Note: this is called at splsoftclock() 1089 */ 1090 static void 1091 kbd_softint(cs) 1092 struct zs_chanstate *cs; 1093 { 1094 register struct kbd_softc *k; 1095 register int get, c, s; 1096 int intr_flags; 1097 register u_short ring_data; 1098 1099 k = cs->cs_private; 1100 1101 /* Atomically get and clear flags. */ 1102 s = splzs(); 1103 intr_flags = k->k_intr_flags; 1104 k->k_intr_flags = 0; 1105 1106 /* Now lower to spltty for the rest. */ 1107 (void) spltty(); 1108 1109 /* 1110 * Copy data from the receive ring to the event layer. 1111 */ 1112 get = k->k_rbget; 1113 while (get != k->k_rbput) { 1114 ring_data = k->k_rbuf[get]; 1115 get = (get + 1) & KBD_RX_RING_MASK; 1116 1117 /* low byte of ring_data is rr1 */ 1118 c = (ring_data >> 8) & 0xff; 1119 1120 if (ring_data & ZSRR1_DO) 1121 intr_flags |= INTR_RX_OVERRUN; 1122 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) { 1123 /* 1124 * After garbage, flush pending input, and 1125 * send a reset to resync key translation. 1126 */ 1127 log(LOG_ERR, "%s: input error (0x%x)\n", 1128 k->k_dev.dv_xname, ring_data); 1129 get = k->k_rbput; /* flush */ 1130 goto send_reset; 1131 } 1132 1133 /* Pass this up to the "middle" layer. */ 1134 kbd_input_raw(k, c); 1135 } 1136 if (intr_flags & INTR_RX_OVERRUN) { 1137 log(LOG_ERR, "%s: input overrun\n", 1138 k->k_dev.dv_xname); 1139 send_reset: 1140 /* Send a reset to resync translation. */ 1141 kbd_output(k, KBD_CMD_RESET); 1142 kbd_start_tx(k); 1143 } 1144 k->k_rbget = get; 1145 1146 if (intr_flags & INTR_TX_EMPTY) { 1147 /* 1148 * Transmit done. Try to send more, or 1149 * clear busy and wakeup drain waiters. 1150 */ 1151 k->k_txflags &= ~K_TXBUSY; 1152 kbd_start_tx(k); 1153 } 1154 1155 if (intr_flags & INTR_ST_CHECK) { 1156 /* 1157 * Status line change. (Not expected.) 1158 */ 1159 log(LOG_ERR, "%s: status interrupt?\n", 1160 k->k_dev.dv_xname); 1161 cs->cs_rr0_delta = 0; 1162 } 1163 1164 splx(s); 1165 } 1166 1167 struct zsops zsops_kbd = { 1168 kbd_rxint, /* receive char available */ 1169 kbd_stint, /* external/status */ 1170 kbd_txint, /* xmit buffer empty */ 1171 kbd_softint, /* process software interrupt */ 1172 }; 1173 1174 /**************************************************************** 1175 * misc... 1176 ****************************************************************/ 1177 1178 /* 1179 * Initialization to be done at first open. 1180 * This is called from kbdopen or kdopen (in kd.c) 1181 * Called with user context. 1182 */ 1183 int 1184 kbd_iopen(unit) 1185 int unit; 1186 { 1187 struct kbd_softc *k; 1188 struct kbd_state *ks; 1189 int error, s; 1190 1191 if (unit >= kbd_cd.cd_ndevs) 1192 return (ENXIO); 1193 k = kbd_cd.cd_devs[unit]; 1194 if (k == NULL) 1195 return (ENXIO); 1196 ks = &k->k_state; 1197 error = 0; 1198 1199 /* Tolerate extra calls. */ 1200 if (k->k_isopen) 1201 return (error); 1202 1203 s = spltty(); 1204 1205 /* Reset the keyboard and find out its type. */ 1206 kbd_output(k, KBD_CMD_RESET); 1207 kbd_start_tx(k); 1208 kbd_drain_tx(k); 1209 /* The wakeup for this is in kbd_was_reset(). */ 1210 error = tsleep((caddr_t)&ks->kbd_id, 1211 PZERO | PCATCH, devopn, hz); 1212 if (error == EWOULDBLOCK) { /* no response */ 1213 error = 0; 1214 log(LOG_ERR, "%s: reset failed\n", 1215 k->k_dev.dv_xname); 1216 /* 1217 * Allow the open anyway (to keep getty happy) 1218 * but assume the "least common denominator". 1219 */ 1220 ks->kbd_id = KB_SUN2; 1221 } 1222 1223 /* Initialize the table pointers for this type. */ 1224 kbd_xlate_init(ks); 1225 1226 /* Earlier than type 4 does not know "layout". */ 1227 if (ks->kbd_id < KB_SUN4) 1228 goto out; 1229 1230 /* Ask for the layout. */ 1231 kbd_output(k, KBD_CMD_GETLAYOUT); 1232 kbd_start_tx(k); 1233 kbd_drain_tx(k); 1234 /* The wakeup for this is in kbd_new_layout(). */ 1235 error = tsleep((caddr_t)&ks->kbd_layout, 1236 PZERO | PCATCH, devopn, hz); 1237 if (error == EWOULDBLOCK) { /* no response */ 1238 error = 0; 1239 log(LOG_ERR, "%s: no response to get_layout\n", 1240 k->k_dev.dv_xname); 1241 ks->kbd_layout = 0; 1242 } 1243 1244 out: 1245 splx(s); 1246 1247 if (error == 0) 1248 k->k_isopen = 1; 1249 1250 return error; 1251 } 1252 1253 /* 1254 * Called by kbd_input_raw, at spltty() 1255 */ 1256 static void 1257 kbd_was_reset(k) 1258 struct kbd_softc *k; 1259 { 1260 struct kbd_state *ks = &k->k_state; 1261 1262 /* 1263 * On first identification, wake up anyone waiting for type 1264 * and set up the table pointers. 1265 */ 1266 wakeup((caddr_t)&ks->kbd_id); 1267 1268 /* Restore keyclick, if necessary */ 1269 switch (ks->kbd_id) { 1270 1271 case KB_SUN2: 1272 /* Type 2 keyboards don't support keyclick */ 1273 break; 1274 1275 case KB_SUN3: 1276 /* Type 3 keyboards come up with keyclick on */ 1277 if (!ks->kbd_click) { 1278 /* turn off the click */ 1279 kbd_output(k, KBD_CMD_NOCLICK); 1280 kbd_start_tx(k); 1281 } 1282 break; 1283 1284 case KB_SUN4: 1285 /* Type 4 keyboards come up with keyclick off */ 1286 if (ks->kbd_click) { 1287 /* turn on the click */ 1288 kbd_output(k, KBD_CMD_CLICK); 1289 kbd_start_tx(k); 1290 } 1291 break; 1292 } 1293 1294 /* LEDs are off after reset. */ 1295 ks->kbd_leds = 0; 1296 } 1297 1298 /* 1299 * Called by kbd_input_raw, at spltty() 1300 */ 1301 static void 1302 kbd_new_layout(k) 1303 struct kbd_softc *k; 1304 { 1305 struct kbd_state *ks = &k->k_state; 1306 1307 /* 1308 * On first identification, wake up anyone waiting for type 1309 * and set up the table pointers. 1310 */ 1311 wakeup((caddr_t)&ks->kbd_layout); 1312 1313 /* XXX: switch decoding tables? */ 1314 } 1315 1316 1317 /* 1318 * Wait for output to finish. 1319 * Called at spltty(). Has user context. 1320 */ 1321 static int 1322 kbd_drain_tx(k) 1323 struct kbd_softc *k; 1324 { 1325 int error; 1326 1327 error = 0; 1328 1329 while (k->k_txflags & K_TXBUSY) { 1330 k->k_txflags |= K_TXWANT; 1331 error = tsleep((caddr_t)&k->k_txflags, 1332 PZERO | PCATCH, "kbdout", 0); 1333 } 1334 1335 return (error); 1336 } 1337 1338 /* 1339 * Enqueue some output for the keyboard 1340 * Called at spltty(). 1341 */ 1342 static void 1343 kbd_output(k, c) 1344 struct kbd_softc *k; 1345 int c; /* the data */ 1346 { 1347 int put; 1348 1349 put = k->k_tbput; 1350 k->k_tbuf[put] = (u_char)c; 1351 put = (put + 1) & KBD_TX_RING_MASK; 1352 1353 /* Would overrun if increment makes (put==get). */ 1354 if (put == k->k_tbget) { 1355 log(LOG_WARNING, "%s: output overrun\n", 1356 k->k_dev.dv_xname); 1357 } else { 1358 /* OK, really increment. */ 1359 k->k_tbput = put; 1360 } 1361 } 1362 1363 /* 1364 * Start the sending data from the output queue 1365 * Called at spltty(). 1366 */ 1367 static void 1368 kbd_start_tx(k) 1369 struct kbd_softc *k; 1370 { 1371 struct zs_chanstate *cs = k->k_cs; 1372 int get, s; 1373 u_char c; 1374 1375 if (k->k_txflags & K_TXBUSY) 1376 return; 1377 1378 /* Is there anything to send? */ 1379 get = k->k_tbget; 1380 if (get == k->k_tbput) { 1381 /* Nothing to send. Wake drain waiters. */ 1382 if (k->k_txflags & K_TXWANT) { 1383 k->k_txflags &= ~K_TXWANT; 1384 wakeup((caddr_t)&k->k_txflags); 1385 } 1386 return; 1387 } 1388 1389 /* Have something to send. */ 1390 c = k->k_tbuf[get]; 1391 get = (get + 1) & KBD_TX_RING_MASK; 1392 k->k_tbget = get; 1393 k->k_txflags |= K_TXBUSY; 1394 1395 /* Need splzs to avoid interruption of the delay. */ 1396 s = splzs(); 1397 zs_write_data(cs, c); 1398 splx(s); 1399 } 1400 1401 /* 1402 * Called at spltty by: 1403 * kbd_update_leds, kbd_iocsled 1404 */ 1405 static void 1406 kbd_set_leds(k, new_leds) 1407 struct kbd_softc *k; 1408 int new_leds; 1409 { 1410 struct kbd_state *ks = &k->k_state; 1411 1412 /* Don't send unless state changes. */ 1413 if (ks->kbd_leds == new_leds) 1414 return; 1415 1416 ks->kbd_leds = new_leds; 1417 1418 /* Only type 4 and later has LEDs anyway. */ 1419 if (ks->kbd_id < KB_SUN4) 1420 return; 1421 1422 kbd_output(k, KBD_CMD_SETLED); 1423 kbd_output(k, new_leds); 1424 kbd_start_tx(k); 1425 } 1426 1427 /* 1428 * Called at spltty by: 1429 * kbd_input_keysym 1430 */ 1431 static void 1432 kbd_update_leds(k) 1433 struct kbd_softc *k; 1434 { 1435 struct kbd_state *ks = &k->k_state; 1436 register char leds; 1437 1438 leds = ks->kbd_leds; 1439 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK); 1440 1441 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) 1442 leds |= LED_CAPS_LOCK; 1443 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) 1444 leds |= LED_NUM_LOCK; 1445 1446 kbd_set_leds(k, leds); 1447 } 1448 1449