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