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