1 /* $NetBSD: kd.c,v 1.20 2001/09/26 20:53:05 eeh 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 * Console driver based on PROM primitives. 41 * 42 * This driver exists to provide a tty device that the indirect 43 * console driver can point to. It also provides a hook that 44 * allows another device to serve console input. This will normally 45 * be a keyboard driver (see sys/dev/sun/kbd.c) 46 */ 47 48 #include "opt_kgdb.h" 49 #include "fb.h" 50 51 #include <sys/param.h> 52 #include <sys/proc.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/ioctl.h> 56 #include <sys/tty.h> 57 #include <sys/file.h> 58 #include <sys/conf.h> 59 #include <sys/device.h> 60 61 #include <machine/bsd_openprom.h> 62 #include <machine/promlib.h> 63 #include <machine/eeprom.h> 64 #include <machine/psl.h> 65 #include <machine/cpu.h> 66 #include <machine/kbd.h> 67 #include <machine/autoconf.h> 68 #include <machine/conf.h> 69 70 #if defined(RASTERCONSOLE) && NFB > 0 71 #include <dev/sun/fbio.h> 72 #include <dev/sun/fbvar.h> 73 #endif 74 75 #include <dev/cons.h> 76 #include <sparc/dev/cons.h> 77 78 #include <dev/sun/event_var.h> 79 #include <dev/sun/kbd_xlate.h> 80 #include <dev/sun/kbdvar.h> 81 82 #define KDMAJOR 1 83 #define PUT_WSIZE 64 84 85 struct kd_softc { 86 struct device kd_dev; /* required first: base device */ 87 struct tty *kd_tty; 88 int rows, cols; 89 90 /* Console input hook */ 91 struct cons_channel *kd_in; 92 }; 93 94 /* 95 * There is no point in pretending there might be 96 * more than one keyboard/display device. 97 */ 98 static struct kd_softc kd_softc; 99 100 static int kdparam(struct tty *, struct termios *); 101 static void kdstart(struct tty *); 102 static void kd_init __P((struct kd_softc *)); 103 static void kd_cons_input __P((int)); 104 105 /* 106 * Prepare the console tty; called on first open of /dev/console 107 */ 108 void 109 kd_init(kd) 110 struct kd_softc *kd; 111 { 112 struct tty *tp; 113 114 tp = ttymalloc(); 115 tp->t_oproc = kdstart; 116 tp->t_param = kdparam; 117 tp->t_dev = makedev(KDMAJOR, 0); 118 119 tty_attach(tp); 120 kd->kd_tty = tp; 121 122 /* 123 * Get the console struct winsize. 124 */ 125 #if defined(RASTERCONSOLE) && NFB > 0 126 /* If the raster console driver is attached, copy its size */ 127 kd->rows = fbrcons_rows(); 128 kd->cols = fbrcons_cols(); 129 rcons_ttyinit(tp); 130 #endif 131 132 /* else, consult the PROM */ 133 switch (prom_version()) { 134 char *prop; 135 struct eeprom *ep; 136 case PROM_OLDMON: 137 if ((ep = (struct eeprom *)eeprom_va) == NULL) 138 break; 139 if (kd->rows == 0) 140 kd->rows = (u_short)ep->eeTtyRows; 141 if (kd->cols == 0) 142 kd->cols = (u_short)ep->eeTtyCols; 143 break; 144 case PROM_OBP_V0: 145 case PROM_OBP_V2: 146 case PROM_OBP_V3: 147 case PROM_OPENFIRM: 148 149 if (kd->rows == 0 && 150 (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) { 151 int i = 0; 152 153 while (*prop != '\0') 154 i = i * 10 + *prop++ - '0'; 155 kd->rows = (unsigned short)i; 156 } 157 if (kd->cols == 0 && 158 (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) { 159 int i = 0; 160 161 while (*prop != '\0') 162 i = i * 10 + *prop++ - '0'; 163 kd->cols = (unsigned short)i; 164 } 165 break; 166 } 167 168 return; 169 } 170 171 struct tty * 172 kdtty(dev) 173 dev_t dev; 174 { 175 struct kd_softc *kd; 176 177 kd = &kd_softc; /* XXX */ 178 return (kd->kd_tty); 179 } 180 181 int 182 kdopen(dev, flag, mode, p) 183 dev_t dev; 184 int flag, mode; 185 struct proc *p; 186 { 187 struct kd_softc *kd; 188 int error, s, unit; 189 struct tty *tp; 190 static int firstopen = 1; 191 192 unit = minor(dev); 193 if (unit != 0) 194 return ENXIO; 195 196 kd = &kd_softc; /* XXX */ 197 if (firstopen) { 198 kd_init(kd); 199 firstopen = 0; 200 } 201 202 tp = kd->kd_tty; 203 204 /* It's simpler to do this up here. */ 205 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) 206 == (TS_ISOPEN | TS_XCLUDE)) 207 && (p->p_ucred->cr_uid != 0) ) 208 { 209 return (EBUSY); 210 } 211 212 s = spltty(); 213 if ((tp->t_state & TS_ISOPEN) == 0) { 214 /* First open. */ 215 216 /* Notify the input device that serves us */ 217 struct cons_channel *cc = kd->kd_in; 218 if (cc != NULL && 219 (error = (*cc->cc_iopen)(cc)) != 0) { 220 return (error); 221 } 222 223 ttychars(tp); 224 tp->t_iflag = TTYDEF_IFLAG; 225 tp->t_oflag = TTYDEF_OFLAG; 226 tp->t_cflag = TTYDEF_CFLAG; 227 tp->t_lflag = TTYDEF_LFLAG; 228 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 229 (void) kdparam(tp, &tp->t_termios); 230 ttsetwater(tp); 231 tp->t_winsize.ws_row = kd->rows; 232 tp->t_winsize.ws_col = kd->cols; 233 /* Flush pending input? Clear translator? */ 234 /* This (pseudo)device always has SOFTCAR */ 235 tp->t_state |= TS_CARR_ON; 236 } 237 238 splx(s); 239 240 return ((*tp->t_linesw->l_open)(dev, tp)); 241 } 242 243 int 244 kdclose(dev, flag, mode, p) 245 dev_t dev; 246 int flag, mode; 247 struct proc *p; 248 { 249 struct kd_softc *kd; 250 struct tty *tp; 251 struct cons_channel *cc; 252 253 kd = &kd_softc; /* XXX */ 254 tp = kd->kd_tty; 255 256 /* XXX This is for cons.c. */ 257 if ((tp->t_state & TS_ISOPEN) == 0) 258 return 0; 259 260 (*tp->t_linesw->l_close)(tp, flag); 261 ttyclose(tp); 262 263 if ((cc = kd->kd_in) != NULL) 264 (void)(*cc->cc_iclose)(cc); 265 266 return (0); 267 } 268 269 int 270 kdread(dev, uio, flag) 271 dev_t dev; 272 struct uio *uio; 273 int flag; 274 { 275 struct kd_softc *kd; 276 struct tty *tp; 277 278 kd = &kd_softc; /* XXX */ 279 tp = kd->kd_tty; 280 281 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 282 } 283 284 int 285 kdwrite(dev, uio, flag) 286 dev_t dev; 287 struct uio *uio; 288 int flag; 289 { 290 struct kd_softc *kd; 291 struct tty *tp; 292 293 kd = &kd_softc; /* XXX */ 294 tp = kd->kd_tty; 295 296 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 297 } 298 299 int 300 kdpoll(dev, events, p) 301 dev_t dev; 302 int events; 303 struct proc *p; 304 { 305 struct kd_softc *kd; 306 struct tty *tp; 307 308 kd = &kd_softc; /* XXX */ 309 tp = kd->kd_tty; 310 311 return ((*tp->t_linesw->l_poll)(tp, events, p)); 312 } 313 314 int 315 kdioctl(dev, cmd, data, flag, p) 316 dev_t dev; 317 u_long cmd; 318 caddr_t data; 319 int flag; 320 struct proc *p; 321 { 322 struct kd_softc *kd; 323 struct tty *tp; 324 int error; 325 326 kd = &kd_softc; /* XXX */ 327 tp = kd->kd_tty; 328 329 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 330 if (error >= 0) 331 return error; 332 error = ttioctl(tp, cmd, data, flag, p); 333 if (error >= 0) 334 return error; 335 336 /* Handle any ioctl commands specific to kbd/display. */ 337 /* XXX - Send KB* ioctls to kbd module? */ 338 /* XXX - Send FB* ioctls to fb module? */ 339 340 return ENOTTY; 341 } 342 343 void 344 kdstop(tp, flag) 345 struct tty *tp; 346 int flag; 347 { 348 349 } 350 351 352 static int 353 kdparam(tp, t) 354 struct tty *tp; 355 struct termios *t; 356 { 357 /* XXX - These are ignored... */ 358 tp->t_ispeed = t->c_ispeed; 359 tp->t_ospeed = t->c_ospeed; 360 tp->t_cflag = t->c_cflag; 361 return 0; 362 } 363 364 365 static void kd_later(void*); 366 static void kd_putfb(struct tty *); 367 368 static void 369 kdstart(tp) 370 struct tty *tp; 371 { 372 struct clist *cl; 373 int s; 374 375 s = spltty(); 376 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 377 goto out; 378 379 cl = &tp->t_outq; 380 if (cl->c_cc) { 381 tp->t_state |= TS_BUSY; 382 if ((s & PSR_PIL) == 0) { 383 /* called at level zero - update screen now. */ 384 (void) spllowersoftclock(); 385 kd_putfb(tp); 386 (void) spltty(); 387 tp->t_state &= ~TS_BUSY; 388 } else { 389 /* called at interrupt level - do it later */ 390 callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp); 391 } 392 } 393 if (cl->c_cc <= tp->t_lowat) { 394 if (tp->t_state & TS_ASLEEP) { 395 tp->t_state &= ~TS_ASLEEP; 396 wakeup((caddr_t)cl); 397 } 398 selwakeup(&tp->t_wsel); 399 } 400 out: 401 splx(s); 402 } 403 404 /* 405 * Timeout function to do delayed writes to the screen. 406 * Called at splsoftclock when requested by kdstart. 407 */ 408 static void 409 kd_later(arg) 410 void *arg; 411 { 412 struct tty *tp = arg; 413 int s; 414 415 kd_putfb(tp); 416 417 s = spltty(); 418 tp->t_state &= ~TS_BUSY; 419 (*tp->t_linesw->l_start)(tp); 420 splx(s); 421 } 422 423 /* 424 * Put text on the screen using the PROM monitor. 425 * This can take a while, so to avoid missing 426 * interrupts, this is called at splsoftclock. 427 */ 428 static void 429 kd_putfb(tp) 430 struct tty *tp; 431 { 432 char buf[PUT_WSIZE]; 433 struct clist *cl = &tp->t_outq; 434 char *p, *end; 435 int len; 436 437 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 438 /* PROM will barf if high bits are set. */ 439 p = buf; 440 end = buf + len; 441 while (p < end) 442 *p++ &= 0x7f; 443 444 /* Now let the PROM print it. */ 445 prom_putstr(buf, len); 446 } 447 } 448 449 /* 450 * Our "interrupt" routine for input. This is called by 451 * the keyboard driver (dev/sun/kbd.c) at spltty. 452 */ 453 void 454 kd_cons_input(c) 455 int c; 456 { 457 struct kd_softc *kd = &kd_softc; 458 struct tty *tp; 459 460 /* XXX: Make sure the device is open. */ 461 tp = kd->kd_tty; 462 if (tp == NULL) 463 return; 464 if ((tp->t_state & TS_ISOPEN) == 0) 465 return; 466 467 (*tp->t_linesw->l_rint)(c, tp); 468 } 469 470 void 471 cons_attach_input(cc, cn) 472 struct cons_channel *cc; 473 struct consdev *cn; 474 { 475 struct kd_softc *kd = &kd_softc; 476 477 kd->kd_in = cc; 478 cc->cc_upstream = kd_cons_input; 479 } 480 481 void kd_attach_input(struct cons_channel *); 482 void 483 kd_attach_input(cc) 484 struct cons_channel *cc; 485 { 486 struct kd_softc *kd = &kd_softc; 487 488 kd->kd_in = cc; 489 cc->cc_upstream = kd_cons_input; 490 } 491 492 /* 493 * Default PROM-based console input stream 494 * Since the PROM does not notify us when data is available on the 495 * input channel these functions periodically poll the PROM. 496 */ 497 static int kd_rom_iopen __P((struct cons_channel *)); 498 static int kd_rom_iclose __P((struct cons_channel *)); 499 static void kd_rom_intr __P((void *)); 500 501 static struct cons_channel prom_cons_channel; 502 503 int 504 kd_rom_iopen(cc) 505 struct cons_channel *cc; 506 { 507 /* Poll for ROM input 4 times per second */ 508 callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc); 509 return (0); 510 } 511 512 int 513 kd_rom_iclose(cc) 514 struct cons_channel *cc; 515 { 516 517 callout_stop(&cc->cc_callout); 518 return (0); 519 } 520 521 /* 522 * "Interrupt" routine for input through ROM vectors 523 */ 524 void 525 kd_rom_intr(arg) 526 void *arg; 527 { 528 struct cons_channel *cc = arg; 529 int s, c; 530 531 /* Re-schedule */ 532 callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc); 533 534 s = spltty(); 535 536 while ((c = prom_peekchar()) >= 0) 537 (*cc->cc_upstream)(c); 538 539 splx(s); 540 } 541 542 /*****************************************************************/ 543 544 int prom_stdin_node; 545 int prom_stdout_node; 546 char prom_stdin_args[16]; 547 char prom_stdout_args[16]; 548 549 extern void prom_cnprobe __P((struct consdev *)); 550 static void prom_cninit __P((struct consdev *)); 551 static int prom_cngetc __P((dev_t)); 552 static void prom_cnputc __P((dev_t, int)); 553 extern void prom_cnpollc __P((dev_t, int)); 554 555 /* 556 * The console is set to this one initially, 557 * which lets us use the PROM until consinit() 558 * is called to select a real console. 559 */ 560 struct consdev consdev_prom = { 561 prom_cnprobe, 562 prom_cninit, 563 prom_cngetc, 564 prom_cnputc, 565 prom_cnpollc, 566 NULL, 567 }; 568 569 /* 570 * The console table pointer is statically initialized 571 * to point to the PROM table, so that early calls to printf will work. 572 */ 573 struct consdev *cn_tab = &consdev_prom; 574 575 void 576 prom_cnprobe(cn) 577 struct consdev *cn; 578 { 579 } 580 581 static void 582 prom_cninit(cn) 583 struct consdev *cn; 584 { 585 } 586 587 void 588 prom_cnpollc(dev, on) 589 dev_t dev; 590 int on; 591 { 592 593 if (on) { 594 /* Entering debugger. */ 595 #if NFB > 0 596 fb_unblank(); 597 #endif 598 } else { 599 /* Resuming kernel. */ 600 } 601 } 602 603 604 /* 605 * PROM console input putchar. 606 */ 607 static int 608 prom_cngetc(dev) 609 dev_t dev; 610 { 611 int s, c; 612 613 s = splhigh(); 614 c = prom_getchar(); 615 splx(s); 616 return (c); 617 } 618 619 /* 620 * PROM console output putchar. 621 */ 622 static void 623 prom_cnputc(dev, c) 624 dev_t dev; 625 int c; 626 { 627 628 prom_putchar(c); 629 } 630 631 632 /*****************************************************************/ 633 634 static void prom_get_device_args __P((const char *, char *, unsigned int)); 635 636 void 637 prom_get_device_args(prop, args, sz) 638 const char *prop; 639 char *args; 640 unsigned int sz; 641 { 642 char *cp, buffer[128]; 643 644 cp = PROM_getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer); 645 646 /* 647 * Extract device-specific arguments from a PROM device path (if any) 648 */ 649 cp = buffer + strlen(buffer); 650 while (cp >= buffer) { 651 if (*cp == ':') { 652 strncpy(args, cp+1, sz); 653 break; 654 } 655 cp--; 656 } 657 } 658 659 /* 660 * 661 */ 662 void 663 consinit() 664 { 665 int inSource, outSink; 666 667 switch (prom_version()) { 668 case PROM_OLDMON: 669 case PROM_OBP_V0: 670 /* The stdio handles identify the device type */ 671 inSource = prom_stdin(); 672 outSink = prom_stdout(); 673 break; 674 675 case PROM_OBP_V2: 676 case PROM_OBP_V3: 677 case PROM_OPENFIRM: 678 679 /* Save PROM arguments for device matching */ 680 prom_get_device_args("stdin-path", prom_stdin_args, 681 sizeof(prom_stdin_args)); 682 prom_get_device_args("stdout-path", prom_stdout_args, 683 sizeof(prom_stdout_args)); 684 685 /* 686 * Translate the STDIO package instance (`ihandle') -- that 687 * the PROM has already opened for us -- to a device tree 688 * node (i.e. a `phandle'). 689 */ 690 691 prom_stdin_node = prom_instance_to_package(prom_stdin()); 692 if (prom_stdin_node == 0) 693 printf("consinit: cannot convert stdin ihandle\n"); 694 695 prom_stdout_node = prom_instance_to_package(prom_stdout()); 696 if (prom_stdout_node == 0) { 697 printf("consinit: cannot convert stdout ihandle\n"); 698 break; 699 } 700 701 break; 702 703 default: 704 break; 705 } 706 707 /* Wire up /dev/console */ 708 cn_tab->cn_dev = makedev(KDMAJOR, 0); 709 cn_tab->cn_pri = CN_INTERNAL; 710 711 /* Set up initial PROM input channel for /dev/console */ 712 prom_cons_channel.cc_dev = NULL; 713 prom_cons_channel.cc_iopen = kd_rom_iopen; 714 prom_cons_channel.cc_iclose = kd_rom_iclose; 715 cons_attach_input(&prom_cons_channel, cn_tab); 716 717 #ifdef KGDB 718 zs_kgdb_init(); /* XXX */ 719 #endif 720 } 721