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