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