1 /* $NetBSD: kd.c,v 1.2 2002/09/06 13:18:43 gehenna Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Keyboard/Display device. 41 * 42 * This driver exists simply to provide a tty device that 43 * the indirect console driver can point to. 44 * The kbd driver sends its input here. 45 * Output goes to the screen via PROM printf. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/ioctl.h> 52 #include <sys/tty.h> 53 #include <sys/file.h> 54 #include <sys/conf.h> 55 #include <sys/device.h> 56 57 #include <machine/promlib.h> 58 #include <machine/eeprom.h> 59 #include <machine/psl.h> 60 #include <machine/cpu.h> 61 #include <machine/kbd.h> 62 #include <machine/autoconf.h> 63 64 #ifdef RASTERCONSOLE 65 #include <dev/sun/fbio.h> 66 #include <machine/fbvar.h> 67 #endif 68 69 70 #include <dev/cons.h> 71 #include <dev/sun/event_var.h> 72 #include <dev/sun/kbd_xlate.h> 73 #include <dev/sun/kbdvar.h> 74 #include <sun2/dev/cons.h> 75 76 struct tty *fbconstty = 0; /* tty structure for frame buffer console */ 77 78 #define PUT_WSIZE 64 79 80 struct kd_softc { 81 struct device kd_dev; /* required first: base device */ 82 struct tty *kd_tty; 83 int rows, cols; 84 85 /* Console input hook */ 86 struct cons_channel *kd_in; 87 }; 88 89 /* 90 * There is no point in pretending there might be 91 * more than one keyboard/display device. 92 */ 93 static struct kd_softc kd_softc; 94 static int kd_is_console; 95 96 static int kdparam(struct tty *, struct termios *); 97 static void kdstart(struct tty *); 98 static void kd_init __P((struct kd_softc *)); 99 static void kd_cons_input __P((int)); 100 static int kdcngetc __P((dev_t)); 101 102 int cons_ocount; /* output byte count */ 103 104 dev_type_open(kdopen); 105 dev_type_close(kdclose); 106 dev_type_read(kdread); 107 dev_type_write(kdwrite); 108 dev_type_ioctl(kdioctl); 109 dev_type_tty(kdtty); 110 dev_type_poll(kdpoll); 111 112 const struct cdevsw kd_cdevsw = { 113 kdopen, kdclose, kdread, kdwrite, kdioctl, 114 nostop, kdtty, kdpoll, nommap, D_TTY 115 }; 116 117 /* 118 * This is called by kbd_attach() 119 * XXX - Make this a proper child of kbd? 120 */ 121 void 122 kd_init(kd) 123 struct kd_softc *kd; 124 { 125 struct tty *tp; 126 #ifdef PROM_OLDMON 127 struct eeprom *ep; 128 #endif 129 #ifdef notyet /* PROM_OBP_V2 */ 130 int i; 131 char *prop; 132 #endif 133 134 kd = &kd_softc; /* XXX */ 135 136 tp = ttymalloc(); 137 tp->t_oproc = kdstart; 138 tp->t_param = kdparam; 139 tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 140 141 tty_attach(tp); 142 kd->kd_tty = tp; 143 144 /* 145 * get the console struct winsize. 146 */ 147 if (kd_is_console) { 148 fbconstty = tp; 149 #ifdef RASTERCONSOLE 150 kd->rows = fbrcons_rows(); 151 kd->cols = fbrcons_cols(); 152 rcons_ttyinit(tp); 153 #endif 154 } 155 156 /* else, consult the PROM */ 157 switch (prom_version()) { 158 #ifdef PROM_OLDMON 159 case PROM_OLDMON: 160 if ((ep = (struct eeprom *)eeprom_va) == NULL) 161 break; 162 if (kd->rows == 0) 163 kd->rows = (u_short)ep->eeTtyRows; 164 if (kd->cols == 0) 165 kd->cols = (u_short)ep->eeTtyCols; 166 break; 167 #endif /* PROM_OLDMON */ 168 #ifdef notyet /* PROM_OBP_V2 */ 169 case PROM_OBP_V0: 170 case PROM_OBP_V2: 171 case PROM_OBP_V3: 172 case PROM_OPENFIRM: 173 174 if (kd->rows == 0 && 175 (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) { 176 i = 0; 177 while (*prop != '\0') 178 i = i * 10 + *prop++ - '0'; 179 kd->rows = (unsigned short)i; 180 } 181 if (kd->cols == 0 && 182 (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) { 183 i = 0; 184 while (*prop != '\0') 185 i = i * 10 + *prop++ - '0'; 186 kd->cols = (unsigned short)i; 187 } 188 break; 189 #endif /* PROM_OBP_V2 */ 190 } 191 return; 192 } 193 194 struct tty * 195 kdtty(dev) 196 dev_t dev; 197 { 198 struct kd_softc *kd; 199 200 kd = &kd_softc; /* XXX */ 201 return (kd->kd_tty); 202 } 203 204 int 205 kdopen(dev, flag, mode, p) 206 dev_t dev; 207 int flag, mode; 208 struct proc *p; 209 { 210 struct kd_softc *kd; 211 int error, s, unit; 212 struct tty *tp; 213 static int firstopen = 1; 214 215 unit = minor(dev); 216 if (unit != 0) 217 return ENXIO; 218 kd = &kd_softc; /* XXX */ 219 220 if (firstopen) { 221 kd_init(kd); 222 firstopen = 0; 223 } 224 tp = kd->kd_tty; 225 226 /* It's simpler to do this up here. */ 227 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) 228 == (TS_ISOPEN | TS_XCLUDE)) 229 && (p->p_ucred->cr_uid != 0) ) 230 { 231 return (EBUSY); 232 } 233 234 s = spltty(); 235 236 if ((tp->t_state & TS_ISOPEN) == 0) { 237 /* First open. */ 238 239 /* Notify the input device that serves us */ 240 struct cons_channel *cc = kd->kd_in; 241 if (cc != NULL && 242 (error = (*cc->cc_iopen)(cc)) != 0) { 243 splx(s); 244 return (error); 245 } 246 247 ttychars(tp); 248 tp->t_iflag = TTYDEF_IFLAG; 249 tp->t_oflag = TTYDEF_OFLAG; 250 tp->t_cflag = TTYDEF_CFLAG; 251 tp->t_lflag = TTYDEF_LFLAG; 252 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 253 (void) kdparam(tp, &tp->t_termios); 254 ttsetwater(tp); 255 tp->t_winsize.ws_row = kd->rows; 256 tp->t_winsize.ws_col = kd->cols; 257 /* Flush pending input? Clear translator? */ 258 /* This (pseudo)device always has SOFTCAR */ 259 tp->t_state |= TS_CARR_ON; 260 } 261 262 splx(s); 263 264 return ((*tp->t_linesw->l_open)(dev, tp)); 265 } 266 267 int 268 kdclose(dev, flag, mode, p) 269 dev_t dev; 270 int flag, mode; 271 struct proc *p; 272 { 273 struct kd_softc *kd; 274 struct tty *tp; 275 struct cons_channel *cc; 276 277 kd = &kd_softc; /* XXX */ 278 tp = kd->kd_tty; 279 280 /* XXX This is for cons.c. */ 281 if ((tp->t_state & TS_ISOPEN) == 0) 282 return 0; 283 284 (*tp->t_linesw->l_close)(tp, flag); 285 ttyclose(tp); 286 287 if ((cc = kd->kd_in) != NULL) 288 (void)(*cc->cc_iclose)(cc->cc_dev); 289 290 return (0); 291 } 292 293 int 294 kdread(dev, uio, flag) 295 dev_t dev; 296 struct uio *uio; 297 int flag; 298 { 299 struct kd_softc *kd; 300 struct tty *tp; 301 302 kd = &kd_softc; /* XXX */ 303 tp = kd->kd_tty; 304 305 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 306 } 307 308 int 309 kdwrite(dev, uio, flag) 310 dev_t dev; 311 struct uio *uio; 312 int flag; 313 { 314 struct kd_softc *kd; 315 struct tty *tp; 316 317 kd = &kd_softc; /* XXX */ 318 tp = kd->kd_tty; 319 320 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 321 } 322 323 int 324 kdpoll(dev, events, p) 325 dev_t dev; 326 int events; 327 struct proc *p; 328 { 329 struct kd_softc *kd; 330 struct tty *tp; 331 332 kd = &kd_softc; /* XXX */ 333 tp = kd->kd_tty; 334 335 return ((*tp->t_linesw->l_poll)(tp, events, p)); 336 } 337 338 int 339 kdioctl(dev, cmd, data, flag, p) 340 dev_t dev; 341 u_long cmd; 342 caddr_t data; 343 int flag; 344 struct proc *p; 345 { 346 struct kd_softc *kd; 347 struct tty *tp; 348 int error; 349 350 kd = &kd_softc; /* XXX */ 351 tp = kd->kd_tty; 352 353 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 354 if (error != EPASSTHROUGH) 355 return error; 356 357 error = ttioctl(tp, cmd, data, flag, p); 358 if (error != EPASSTHROUGH) 359 return error; 360 361 /* Handle any ioctl commands specific to kbd/display. */ 362 /* XXX - Send KB* ioctls to kbd module? */ 363 /* XXX - Send FB* ioctls to fb module? */ 364 365 return EPASSTHROUGH; 366 } 367 368 static int 369 kdparam(tp, t) 370 struct tty *tp; 371 struct termios *t; 372 { 373 /* XXX - These are ignored... */ 374 tp->t_ispeed = t->c_ispeed; 375 tp->t_ospeed = t->c_ospeed; 376 tp->t_cflag = t->c_cflag; 377 return 0; 378 } 379 380 381 static void kd_later(void*); 382 static void kd_putfb(struct tty *); 383 384 static void 385 kdstart(tp) 386 struct tty *tp; 387 { 388 struct clist *cl; 389 register int s; 390 391 s = spltty(); 392 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 393 goto out; 394 395 cl = &tp->t_outq; 396 if (cl->c_cc) { 397 if (kd_is_console) { 398 tp->t_state |= TS_BUSY; 399 if (is_spl0(s)) { 400 /* called at level zero - update screen now. */ 401 (void) spllowersoftclock(); 402 kd_putfb(tp); 403 (void) spltty(); 404 tp->t_state &= ~TS_BUSY; 405 } else { 406 /* called at interrupt level - do it later */ 407 callout_reset(&tp->t_rstrt_ch, 0, 408 kd_later, tp); 409 } 410 } else { 411 /* 412 * This driver uses the PROM for writing the screen, 413 * and that only works if this is the console device. 414 * If this is not the console, just flush the output. 415 * Sorry. (In that case, use xdm instead of getty.) 416 */ 417 ndflush(cl, cl->c_cc); 418 } 419 } 420 if (cl->c_cc <= tp->t_lowat) { 421 if (tp->t_state & TS_ASLEEP) { 422 tp->t_state &= ~TS_ASLEEP; 423 wakeup((caddr_t)cl); 424 } 425 selwakeup(&tp->t_wsel); 426 } 427 out: 428 splx(s); 429 } 430 431 /* 432 * Timeout function to do delayed writes to the screen. 433 * Called at splsoftclock when requested by kdstart. 434 */ 435 static void 436 kd_later(tpaddr) 437 void *tpaddr; 438 { 439 struct tty *tp = tpaddr; 440 register int s; 441 442 kd_putfb(tp); 443 444 s = spltty(); 445 tp->t_state &= ~TS_BUSY; 446 (*tp->t_linesw->l_start)(tp); 447 splx(s); 448 } 449 450 /* 451 * Put text on the screen using the PROM monitor. 452 * This can take a while, so to avoid missing 453 * interrupts, this is called at splsoftclock. 454 */ 455 static void 456 kd_putfb(tp) 457 struct tty *tp; 458 { 459 char buf[PUT_WSIZE]; 460 struct clist *cl = &tp->t_outq; 461 char *p, *end; 462 int len; 463 464 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 465 /* PROM will barf if high bits are set. */ 466 p = buf; 467 end = buf + len; 468 while (p < end) 469 *p++ &= 0x7f; 470 /* Now let the PROM print it. */ 471 prom_putstr(buf, len); 472 } 473 } 474 475 /* 476 * Default PROM-based console input stream 477 */ 478 static int kd_rom_iopen __P((struct cons_channel *)); 479 static int kd_rom_iclose __P((struct cons_channel *)); 480 481 static struct cons_channel prom_cons_channel; 482 483 int 484 kd_rom_iopen(cc) 485 struct cons_channel *cc; 486 { 487 return (0); 488 } 489 490 int 491 kd_rom_iclose(cc) 492 struct cons_channel *cc; 493 { 494 return (0); 495 } 496 497 /* 498 * Our "interrupt" routine for input. This is called by 499 * the keyboard driver (dev/sun/kbd.c) at spltty. 500 */ 501 void 502 kd_cons_input(c) 503 int c; 504 { 505 struct kd_softc *kd = &kd_softc; 506 struct tty *tp; 507 508 /* XXX: Make sure the device is open. */ 509 tp = kd->kd_tty; 510 if (tp == NULL) 511 return; 512 if ((tp->t_state & TS_ISOPEN) == 0) 513 return; 514 515 (*tp->t_linesw->l_rint)(c, tp); 516 } 517 518 519 /**************************************************************** 520 * kd console support 521 ****************************************************************/ 522 523 /* The debugger gets its own key translation state. */ 524 static struct kbd_state *kdcn_state; 525 526 static void kdcnprobe __P((struct consdev *)); 527 static void kdcninit __P((struct consdev *)); 528 static void kdcnputc __P((dev_t, int)); 529 static void kdcnpollc __P((dev_t, int)); 530 531 /* The keyboard driver uses cn_hw to access the real console driver */ 532 extern struct consdev consdev_prom; 533 struct consdev consdev_kd = { 534 kdcnprobe, 535 kdcninit, 536 kdcngetc, 537 kdcnputc, 538 kdcnpollc, 539 NULL, 540 }; 541 struct consdev *cn_hw = &consdev_kd; 542 543 void 544 cons_attach_input(cc, cn) 545 struct cons_channel *cc; 546 struct consdev *cn; 547 { 548 struct kd_softc *kd = &kd_softc; 549 struct kbd_softc *kds = cc->cc_dev; 550 struct kbd_state *ks; 551 552 /* Share the keyboard state */ 553 kdcn_state = ks = &kds->k_state; 554 555 kd->kd_in = cc; 556 cc->cc_upstream = kd_cons_input; 557 558 /* Attach lower level. */ 559 cn_hw->cn_dev = cn->cn_dev; 560 cn_hw->cn_pollc = cn->cn_pollc; 561 cn_hw->cn_getc = cn->cn_getc; 562 563 /* Attach us as console. */ 564 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 565 cn_tab->cn_probe = kdcnprobe; 566 cn_tab->cn_init = kdcninit; 567 cn_tab->cn_getc = kdcngetc; 568 cn_tab->cn_pollc = kdcnpollc; 569 cn_tab->cn_pri = CN_INTERNAL; 570 571 /* Set up initial PROM input channel for /dev/console */ 572 prom_cons_channel.cc_dev = NULL; 573 prom_cons_channel.cc_iopen = kd_rom_iopen; 574 prom_cons_channel.cc_iclose = kd_rom_iclose; 575 576 /* Indicate that it is OK to use the PROM fbwrite */ 577 kd_is_console = 1; 578 } 579 580 581 void kd_attach_input(struct cons_channel *); 582 void 583 kd_attach_input(cc) 584 struct cons_channel *cc; 585 { 586 struct kd_softc *kd = &kd_softc; 587 588 kd->kd_in = cc; 589 cc->cc_upstream = kd_cons_input; 590 } 591 592 593 /* We never call this. */ 594 static void 595 kdcnprobe(cn) 596 struct consdev *cn; 597 { 598 } 599 600 static void 601 kdcninit(cn) 602 struct consdev *cn; 603 { 604 #if 0 605 struct kbd_state *ks = kdcn_state; 606 607 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 608 cn->cn_pri = CN_INTERNAL; 609 610 /* This prepares kbd_translate() */ 611 ks->kbd_id = KBD_MIN_TYPE; 612 kbd_xlate_init(ks); 613 614 /* Set up initial PROM input channel for /dev/console */ 615 prom_cons_channel.cc_dev = NULL; 616 prom_cons_channel.cc_iopen = kd_rom_iopen; 617 prom_cons_channel.cc_iclose = kd_rom_iclose; 618 cons_attach_input(&prom_cons_channel); 619 620 /* Indicate that it is OK to use the PROM fbwrite */ 621 kd_is_console = 1; 622 #endif 623 } 624 625 static int 626 kdcngetc(dev) 627 dev_t dev; 628 { 629 struct kbd_state *ks = kdcn_state; 630 int code, class, data, keysym; 631 extern int prom_cngetc __P((dev_t)); 632 633 634 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev); 635 for (;;) { 636 code = (*cn_hw->cn_getc)(dev); 637 keysym = kbd_code_to_keysym(ks, code); 638 class = KEYSYM_CLASS(keysym); 639 640 switch (class) { 641 case KEYSYM_ASCII: 642 goto out; 643 644 case KEYSYM_CLRMOD: 645 case KEYSYM_SETMOD: 646 data = (keysym & 0x1F); 647 /* Only allow ctrl or shift. */ 648 if (data > KBMOD_SHIFT_R) 649 break; 650 data = 1 << data; 651 if (class == KEYSYM_SETMOD) 652 ks->kbd_modbits |= data; 653 else 654 ks->kbd_modbits &= ~data; 655 break; 656 657 case KEYSYM_ALL_UP: 658 /* No toggle keys here. */ 659 ks->kbd_modbits = 0; 660 break; 661 662 default: /* ignore all other keysyms */ 663 break; 664 } 665 } 666 out: 667 return (keysym); 668 } 669 670 static void 671 kdcnputc(dev, c) 672 dev_t dev; 673 int c; 674 { 675 int s; 676 677 s = splhigh(); 678 prom_putchar(c); 679 splx(s); 680 } 681 682 static void 683 kdcnpollc(dev, on) 684 dev_t dev; 685 int on; 686 { 687 struct kbd_state *ks = kdcn_state; 688 689 if (on) { 690 /* Entering debugger. */ 691 #if NFB > 0 692 fb_unblank(); 693 #endif 694 /* Clear shift keys too. */ 695 ks->kbd_modbits = 0; 696 } else { 697 /* Resuming kernel. */ 698 } 699 (*cn_hw->cn_pollc)(dev, on); 700 } 701