1 /* $NetBSD: kd.c,v 1.54 2014/07/25 08:10:34 dholland 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.54 2014/07/25 08:10:34 dholland 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 tty *kd_tty; 82 int rows, cols; 83 84 /* Console input hook */ 85 struct cons_channel *kd_in; 86 }; 87 88 /* 89 * There is no point in pretending there might be 90 * more than one keyboard/display device. 91 */ 92 static struct kd_softc kd_softc; 93 94 /* For keyboard driver to register itself as console input */ 95 void kd_attach_input(struct cons_channel *); 96 97 static void kd_init(struct kd_softc *); 98 static void kdstart(struct tty *); 99 static void kd_later(void *); 100 static void kd_putfb(struct tty *); 101 static int kdparam(struct tty *, struct termios *); 102 static void kd_cons_input(int); 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 .d_open = kdopen, 114 .d_close = kdclose, 115 .d_read = kdread, 116 .d_write = kdwrite, 117 .d_ioctl = kdioctl, 118 .d_stop = nostop, 119 .d_tty = kdtty, 120 .d_poll = kdpoll, 121 .d_mmap = nommap, 122 .d_kqfilter = ttykqfilter, 123 .d_discard = nodiscard, 124 .d_flag = D_TTY 125 }; 126 127 /* 128 * Prepare the console tty; called on first open of /dev/console 129 */ 130 static void 131 kd_init(struct kd_softc *kd) 132 { 133 struct tty *tp; 134 135 tp = tty_alloc(); 136 callout_setfunc(&tp->t_rstrt_ch, kd_later, tp); 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 defined(RASTERCONSOLE) && NFB > 0 148 /* If the raster console driver is attached, copy its size */ 149 kd->rows = fbrcons_rows(); 150 kd->cols = fbrcons_cols(); 151 rcons_ttyinit(tp); 152 #endif 153 154 /* else, consult the PROM */ 155 switch (prom_version()) { 156 char prop[6+1]; /* Enough for six digits */ 157 struct eeprom *ep; 158 case PROM_OLDMON: 159 if ((ep = (struct eeprom *)eeprom_va) == NULL) 160 break; 161 if (kd->rows == 0) 162 kd->rows = (u_short)ep->eeTtyRows; 163 if (kd->cols == 0) 164 kd->cols = (u_short)ep->eeTtyCols; 165 break; 166 167 case PROM_OBP_V0: 168 case PROM_OBP_V2: 169 case PROM_OBP_V3: 170 case PROM_OPENFIRM: 171 if (kd->rows == 0 && 172 prom_getoption("screen-#rows", prop, sizeof prop) == 0) 173 kd->rows = strtoul(prop, NULL, 10); 174 175 if (kd->cols == 0 && 176 prom_getoption("screen-#columns", prop, sizeof prop) == 0) 177 kd->cols = strtoul(prop, NULL, 10); 178 179 break; 180 } 181 182 return; 183 } 184 185 struct tty * 186 kdtty(dev_t dev) 187 { 188 struct kd_softc *kd; 189 190 kd = &kd_softc; /* XXX */ 191 return (kd->kd_tty); 192 } 193 194 int 195 kdopen(dev_t dev, int flag, int mode, struct lwp *l) 196 { 197 struct kd_softc *kd; 198 int error, s, unit; 199 struct tty *tp; 200 static int firstopen = 1; 201 202 unit = minor(dev); 203 if (unit != 0) 204 return ENXIO; 205 206 kd = &kd_softc; /* XXX */ 207 if (firstopen) { 208 kd_init(kd); 209 firstopen = 0; 210 } 211 212 tp = kd->kd_tty; 213 214 /* It's simpler to do this up here. */ 215 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 216 return (EBUSY); 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, void *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 int s1, s2; 348 349 s1 = splsoftclock(); 350 s2 = spltty(); 351 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 352 goto out; 353 354 if (ttypull(tp)) { 355 tp->t_state |= TS_BUSY; 356 if ((s1 & PSR_PIL) == 0) { 357 /* called at level zero - update screen now. */ 358 splx(s2); 359 kd_putfb(tp); 360 s2 = spltty(); 361 tp->t_state &= ~TS_BUSY; 362 } else { 363 /* called at interrupt level - do it later */ 364 callout_schedule(&tp->t_rstrt_ch, 0); 365 } 366 } 367 out: 368 splx(s2); 369 splx(s1); 370 } 371 372 /* 373 * Timeout function to do delayed writes to the screen. 374 * Called at splsoftclock when requested by kdstart. 375 */ 376 static void 377 kd_later(void *arg) 378 { 379 struct tty *tp = arg; 380 int s; 381 382 kd_putfb(tp); 383 384 s = spltty(); 385 tp->t_state &= ~TS_BUSY; 386 (*tp->t_linesw->l_start)(tp); 387 splx(s); 388 } 389 390 /* 391 * Put text on the screen using the PROM monitor. 392 * This can take a while, so to avoid missing 393 * interrupts, this is called at splsoftclock. 394 */ 395 static void 396 kd_putfb(struct tty *tp) 397 { 398 char buf[PUT_WSIZE]; 399 struct clist *cl = &tp->t_outq; 400 char *p, *end; 401 int len; 402 403 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 404 /* PROM will barf if high bits are set. */ 405 p = buf; 406 end = buf + len; 407 while (p < end) 408 *p++ &= 0x7f; 409 410 /* Now let the PROM print it. */ 411 prom_putstr(buf, len); 412 } 413 } 414 415 /* 416 * Our "interrupt" routine for input. This is called by 417 * the keyboard driver (dev/sun/kbd.c) at spltty. 418 */ 419 static void 420 kd_cons_input(int c) 421 { 422 struct kd_softc *kd = &kd_softc; 423 struct tty *tp; 424 425 /* XXX: Make sure the device is open. */ 426 tp = kd->kd_tty; 427 if (tp == NULL) 428 return; 429 if ((tp->t_state & TS_ISOPEN) == 0) 430 return; 431 432 (*tp->t_linesw->l_rint)(c, tp); 433 } 434 435 void 436 cons_attach_input(struct cons_channel *cc, struct consdev *cn) 437 { 438 struct kd_softc *kd = &kd_softc; 439 440 kd->kd_in = cc; 441 cc->cc_upstream = kd_cons_input; 442 } 443 444 void 445 kd_attach_input(struct cons_channel *cc) 446 { 447 struct kd_softc *kd = &kd_softc; 448 449 kd->kd_in = cc; 450 cc->cc_upstream = kd_cons_input; 451 } 452 453 /* 454 * Default PROM-based console input stream 455 * Since the PROM does not notify us when data is available on the 456 * input channel these functions periodically poll the PROM. 457 */ 458 static int kd_rom_iopen(struct cons_channel *); 459 static int kd_rom_iclose(struct cons_channel *); 460 static void kd_rom_intr(void *); 461 462 static struct cons_channel prom_cons_channel = { 463 NULL, /* no private data */ 464 kd_rom_iopen, 465 kd_rom_iclose, 466 NULL /* will be set by kd driver */ 467 }; 468 469 static struct callout prom_cons_callout; 470 471 static int 472 kd_rom_iopen(struct cons_channel *cc) 473 { 474 static bool callo; 475 476 if (!callo) { 477 callout_init(&prom_cons_callout, 0); 478 callo = true; 479 } 480 481 /* Poll for ROM input 4 times per second */ 482 callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc); 483 return (0); 484 } 485 486 static int 487 kd_rom_iclose(struct cons_channel *cc) 488 { 489 490 callout_stop(&prom_cons_callout); 491 return (0); 492 } 493 494 /* 495 * "Interrupt" routine for input through ROM vectors 496 */ 497 static void 498 kd_rom_intr(void *arg) 499 { 500 struct cons_channel *cc = arg; 501 int s, c; 502 503 callout_schedule(&prom_cons_callout, hz / 4); 504 505 s = spltty(); 506 507 while ((c = prom_peekchar()) >= 0) 508 (*cc->cc_upstream)(c); 509 510 splx(s); 511 } 512 513 /*****************************************************************/ 514 515 int prom_stdin_node; 516 int prom_stdout_node; 517 char prom_stdin_args[16]; 518 char prom_stdout_args[16]; 519 520 static void prom_cnprobe(struct consdev *); 521 static void prom_cninit(struct consdev *); 522 int prom_cngetc(dev_t); /* XXX: for sunkbd_wskbd_cngetc */ 523 static void prom_cnputc(dev_t, int); 524 static void prom_cnpollc(dev_t, int); 525 526 /* 527 * The console is set to this one initially, 528 * which lets us use the PROM until consinit() 529 * is called to select a real console. 530 */ 531 struct consdev consdev_prom = { 532 prom_cnprobe, 533 prom_cninit, 534 prom_cngetc, 535 prom_cnputc, 536 prom_cnpollc, 537 NULL, 538 }; 539 540 /* 541 * The console table pointer is statically initialized 542 * to point to the PROM table, so that early calls to printf will work. 543 * this has been moved to cpu_startup() 544 */ 545 #if 0 546 struct consdev *cn_tab = &consdev_prom; 547 #endif 548 549 static void 550 prom_cnprobe(struct consdev *cn) 551 { 552 } 553 554 static void 555 prom_cninit(struct consdev *cn) 556 { 557 } 558 559 static void 560 prom_cnpollc(dev_t dev, int on) 561 { 562 563 if (on) { 564 /* Entering debugger. */ 565 #if NFB > 0 566 fb_unblank(); 567 #endif 568 } else { 569 /* Resuming kernel. */ 570 } 571 } 572 573 574 /* 575 * PROM console input putchar. 576 */ 577 int 578 prom_cngetc(dev_t dev) 579 { 580 int s, c; 581 582 s = splhigh(); 583 c = prom_getchar(); 584 splx(s); 585 return (c); 586 } 587 588 /* 589 * PROM console output putchar. 590 */ 591 static void 592 prom_cnputc(dev_t dev, int c) 593 { 594 595 prom_putchar(c); 596 } 597 598 599 /*****************************************************************/ 600 601 static void prom_get_device_args(const char *, char *, unsigned int); 602 603 static void 604 prom_get_device_args(const char *prop, char *args, unsigned int sz) 605 { 606 const char *cp; 607 char buffer[128]; 608 609 cp = prom_getpropstringA(findroot(), prop, buffer, sizeof buffer); 610 611 /* 612 * Extract device-specific arguments from a PROM device path (if any) 613 */ 614 cp = buffer + strlen(buffer); 615 while (cp >= buffer) { 616 if (*cp == ':') { 617 strncpy(args, cp+1, sz); 618 break; 619 } 620 cp--; 621 } 622 } 623 624 /* 625 * 626 */ 627 void 628 consinit(void) 629 { 630 #if 0 631 int inSource, outSink; 632 #endif 633 634 switch (prom_version()) { 635 #if 0 636 case PROM_OLDMON: 637 case PROM_OBP_V0: 638 /* The stdio handles identify the device type */ 639 inSource = prom_stdin(); 640 outSink = prom_stdout(); 641 break; 642 // XXXMRG should these just set prom_stdin_node / prom_stdout_node? 643 #endif 644 645 case PROM_OBP_V2: 646 case PROM_OBP_V3: 647 case PROM_OPENFIRM: 648 649 /* Save PROM arguments for device matching */ 650 prom_get_device_args("stdin-path", prom_stdin_args, 651 sizeof(prom_stdin_args)); 652 prom_get_device_args("stdout-path", prom_stdout_args, 653 sizeof(prom_stdout_args)); 654 655 /* 656 * Translate the STDIO package instance (`ihandle') -- that 657 * the PROM has already opened for us -- to a device tree 658 * node (i.e. a `phandle'). 659 */ 660 661 prom_stdin_node = prom_instance_to_package(prom_stdin()); 662 if (prom_stdin_node == 0) 663 printf("consinit: cannot convert stdin ihandle\n"); 664 665 prom_stdout_node = prom_instance_to_package(prom_stdout()); 666 if (prom_stdout_node == 0) { 667 printf("consinit: cannot convert stdout ihandle\n"); 668 break; 669 } 670 671 break; 672 673 default: 674 break; 675 } 676 677 /* Wire up /dev/console */ 678 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 679 cn_tab->cn_pri = CN_INTERNAL; 680 681 /* Set up initial PROM input channel for /dev/console */ 682 cons_attach_input(&prom_cons_channel, cn_tab); 683 684 #ifdef KGDB 685 zs_kgdb_init(); /* XXX */ 686 #endif 687 } 688