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