1 /* $NetBSD: kd.c,v 1.35 2005/12/11 12:19:09 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 * Keyboard/Display device. 41 * 42 * This driver exists simply to provide a tty device that 43 * the indirect console driver can point to. 44 * The kbd driver sends its input here. 45 * Output goes to the screen via PROM printf. 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.35 2005/12/11 12:19:09 christos Exp $"); 50 51 #include <sys/param.h> 52 #include <sys/proc.h> 53 #include <sys/systm.h> 54 #include <sys/ioctl.h> 55 #include <sys/tty.h> 56 #include <sys/file.h> 57 #include <sys/conf.h> 58 #include <sys/device.h> 59 60 #include <machine/openfirm.h> 61 #include <machine/eeprom.h> 62 #include <machine/psl.h> 63 #include <machine/cpu.h> 64 #include <machine/kbd.h> 65 #include <machine/autoconf.h> 66 67 #include <dev/cons.h> 68 #include <dev/sun/event_var.h> 69 #include <dev/sun/kbd_xlate.h> 70 #include <dev/sun/kbdvar.h> 71 #include <sparc64/dev/cons.h> 72 73 dev_type_open(kdopen); 74 dev_type_close(kdclose); 75 dev_type_read(kdread); 76 dev_type_write(kdwrite); 77 dev_type_ioctl(kdioctl); 78 dev_type_tty(kdtty); 79 dev_type_poll(kdpoll); 80 81 const struct cdevsw kd_cdevsw = { 82 kdopen, kdclose, kdread, kdwrite, kdioctl, 83 nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY 84 }; 85 86 struct tty *fbconstty = 0; /* tty structure for frame buffer console */ 87 88 #define PUT_WSIZE 64 89 90 struct kd_softc { 91 struct device kd_dev; /* required first: base device */ 92 struct tty *kd_tty; 93 int rows, cols; 94 95 /* Console input hook */ 96 struct cons_channel *kd_in; 97 }; 98 99 /* 100 * There is no point in pretending there might be 101 * more than one keyboard/display device. 102 */ 103 static struct kd_softc kd_softc; 104 static int kd_is_console; 105 106 static int kdparam(struct tty *, struct termios *); 107 static void kdstart(struct tty *); 108 static void kd_init __P((struct kd_softc *)); 109 static void kd_cons_input __P((int)); 110 static int kdcngetc __P((dev_t)); 111 112 int cons_ocount; /* output byte count */ 113 114 /* 115 * This is called by kbd_attach() 116 * XXX - Make this a proper child of kbd? 117 */ 118 void 119 kd_init(kd) 120 struct kd_softc *kd; 121 { 122 struct tty *tp; 123 char prop[6+1]; 124 125 kd = &kd_softc; /* XXX */ 126 127 tp = ttymalloc(); 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 (kd_is_console) { 139 fbconstty = tp; 140 } 141 142 if (kd->rows == 0 && 143 prom_getoption("screen-#rows", prop, sizeof prop) == 0) 144 kd->rows = strtoul(prop, NULL, 10); 145 146 if (kd->cols == 0 && 147 prom_getoption("screen-#columns", prop, sizeof prop) == 0) 148 kd->cols = strtoul(prop, NULL, 10); 149 } 150 151 struct tty * 152 kdtty(dev) 153 dev_t dev; 154 { 155 struct kd_softc *kd; 156 157 kd = &kd_softc; /* XXX */ 158 return (kd->kd_tty); 159 } 160 161 int 162 kdopen(dev, flag, mode, l) 163 dev_t dev; 164 int flag, mode; 165 struct lwp *l; 166 { 167 struct kd_softc *kd; 168 int error, s, unit; 169 struct tty *tp; 170 struct proc *p = l->l_proc; 171 static int firstopen = 1; 172 173 unit = minor(dev); 174 if (unit != 0) 175 return ENXIO; 176 kd = &kd_softc; /* XXX */ 177 178 if (firstopen) { 179 kd_init(kd); 180 firstopen = 0; 181 } 182 tp = kd->kd_tty; 183 184 /* It's simpler to do this up here. */ 185 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) 186 == (TS_ISOPEN | TS_XCLUDE)) 187 && (suser(p->p_ucred, &p->p_acflag) != 0) ) 188 { 189 return (EBUSY); 190 } 191 192 s = spltty(); 193 194 if ((tp->t_state & TS_ISOPEN) == 0) { 195 /* First open. */ 196 197 /* Notify the input device that serves us */ 198 struct cons_channel *cc = kd->kd_in; 199 if (cc != NULL && 200 (error = (*cc->cc_iopen)(cc)) != 0) { 201 splx(s); 202 return (error); 203 } 204 205 ttychars(tp); 206 tp->t_iflag = TTYDEF_IFLAG; 207 tp->t_oflag = TTYDEF_OFLAG; 208 tp->t_cflag = TTYDEF_CFLAG; 209 tp->t_lflag = TTYDEF_LFLAG; 210 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 211 (void) kdparam(tp, &tp->t_termios); 212 ttsetwater(tp); 213 tp->t_winsize.ws_row = kd->rows; 214 tp->t_winsize.ws_col = kd->cols; 215 /* Flush pending input? Clear translator? */ 216 /* This (pseudo)device always has SOFTCAR */ 217 tp->t_state |= TS_CARR_ON; 218 } 219 220 splx(s); 221 222 return ((*tp->t_linesw->l_open)(dev, tp)); 223 } 224 225 int 226 kdclose(dev, flag, mode, l) 227 dev_t dev; 228 int flag, mode; 229 struct lwp *l; 230 { 231 struct kd_softc *kd; 232 struct tty *tp; 233 struct cons_channel *cc; 234 235 kd = &kd_softc; /* XXX */ 236 tp = kd->kd_tty; 237 238 /* XXX This is for cons.c. */ 239 if ((tp->t_state & TS_ISOPEN) == 0) 240 return 0; 241 242 (*tp->t_linesw->l_close)(tp, flag); 243 ttyclose(tp); 244 245 if ((cc = kd->kd_in) != NULL) 246 (void)(*cc->cc_iclose)(cc); 247 248 return (0); 249 } 250 251 int 252 kdread(dev, uio, flag) 253 dev_t dev; 254 struct uio *uio; 255 int flag; 256 { 257 struct kd_softc *kd; 258 struct tty *tp; 259 260 kd = &kd_softc; /* XXX */ 261 tp = kd->kd_tty; 262 263 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 264 } 265 266 int 267 kdwrite(dev, uio, flag) 268 dev_t dev; 269 struct uio *uio; 270 int flag; 271 { 272 struct kd_softc *kd; 273 struct tty *tp; 274 275 kd = &kd_softc; /* XXX */ 276 tp = kd->kd_tty; 277 278 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 279 } 280 281 int 282 kdpoll(dev, events, l) 283 dev_t dev; 284 int events; 285 struct lwp *l; 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_poll)(tp, events, l)); 294 } 295 296 int 297 kdioctl(dev, cmd, data, flag, l) 298 dev_t dev; 299 u_long cmd; 300 caddr_t data; 301 int flag; 302 struct lwp *l; 303 { 304 struct kd_softc *kd; 305 struct tty *tp; 306 int error; 307 308 kd = &kd_softc; /* XXX */ 309 tp = kd->kd_tty; 310 311 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 312 if (error != EPASSTHROUGH) 313 return error; 314 315 error = ttioctl(tp, cmd, data, flag, l); 316 if (error != EPASSTHROUGH) 317 return error; 318 319 /* Handle any ioctl commands specific to kbd/display. */ 320 /* XXX - Send KB* ioctls to kbd module? */ 321 /* XXX - Send FB* ioctls to fb module? */ 322 323 return EPASSTHROUGH; 324 } 325 326 static int 327 kdparam(tp, t) 328 struct tty *tp; 329 struct termios *t; 330 { 331 /* XXX - These are ignored... */ 332 tp->t_ispeed = t->c_ispeed; 333 tp->t_ospeed = t->c_ospeed; 334 tp->t_cflag = t->c_cflag; 335 return 0; 336 } 337 338 339 static void kd_later(void*); 340 static void kd_putfb(struct tty *); 341 342 static void 343 kdstart(tp) 344 struct tty *tp; 345 { 346 struct clist *cl; 347 register int s; 348 349 s = spltty(); 350 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 351 goto out; 352 353 cl = &tp->t_outq; 354 if (cl->c_cc) { 355 if (kd_is_console) { 356 tp->t_state |= TS_BUSY; 357 if (s == 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, 366 kd_later, tp); 367 } 368 } else { 369 /* 370 * This driver uses the PROM for writing the screen, 371 * and that only works if this is the console device. 372 * If this is not the console, just flush the output. 373 * Sorry. (In that case, use xdm instead of getty.) 374 */ 375 ndflush(cl, cl->c_cc); 376 } 377 } 378 if (cl->c_cc <= tp->t_lowat) { 379 if (tp->t_state & TS_ASLEEP) { 380 tp->t_state &= ~TS_ASLEEP; 381 wakeup((caddr_t)cl); 382 } 383 selwakeup(&tp->t_wsel); 384 } 385 out: 386 splx(s); 387 } 388 389 /* 390 * Timeout function to do delayed writes to the screen. 391 * Called at splsoftclock when requested by kdstart. 392 */ 393 static void 394 kd_later(tpaddr) 395 void *tpaddr; 396 { 397 struct tty *tp = tpaddr; 398 register int s; 399 400 kd_putfb(tp); 401 402 s = spltty(); 403 tp->t_state &= ~TS_BUSY; 404 (*tp->t_linesw->l_start)(tp); 405 splx(s); 406 } 407 408 /* 409 * Put text on the screen using the PROM monitor. 410 * This can take a while, so to avoid missing 411 * interrupts, this is called at splsoftclock. 412 */ 413 static void 414 kd_putfb(tp) 415 struct tty *tp; 416 { 417 char buf[PUT_WSIZE]; 418 struct clist *cl = &tp->t_outq; 419 char *p, *end; 420 int len; 421 422 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 423 /* PROM will barf if high bits are set. */ 424 p = buf; 425 end = buf + len; 426 while (p < end) 427 *p++ &= 0x7f; 428 /* Now let the PROM print it. */ 429 prom_write(prom_stdout(), buf, len); 430 } 431 } 432 433 /* 434 * Default PROM-based console input stream 435 */ 436 static int kd_rom_iopen __P((struct cons_channel *)); 437 static int kd_rom_iclose __P((struct cons_channel *)); 438 439 static struct cons_channel prom_cons_channel; 440 441 int 442 kd_rom_iopen(cc) 443 struct cons_channel *cc; 444 { 445 return (0); 446 } 447 448 int 449 kd_rom_iclose(cc) 450 struct cons_channel *cc; 451 { 452 return (0); 453 } 454 455 /* 456 * Our "interrupt" routine for input. This is called by 457 * the keyboard driver (dev/sun/kbd.c) at spltty. 458 */ 459 void 460 kd_cons_input(c) 461 int c; 462 { 463 struct kd_softc *kd = &kd_softc; 464 struct tty *tp; 465 466 /* XXX: Make sure the device is open. */ 467 tp = kd->kd_tty; 468 if (tp == NULL) 469 return; 470 if ((tp->t_state & TS_ISOPEN) == 0) 471 return; 472 473 (*tp->t_linesw->l_rint)(c, tp); 474 } 475 476 477 /**************************************************************** 478 * kd console support 479 ****************************************************************/ 480 481 /* The debugger gets its own key translation state. */ 482 static struct kbd_state *kdcn_state; 483 484 static void kdcnprobe __P((struct consdev *)); 485 static void kdcninit __P((struct consdev *)); 486 static void kdcnputc __P((dev_t, int)); 487 static void kdcnpollc __P((dev_t, int)); 488 489 /* The keyboard driver uses cn_hw to access the real console driver */ 490 extern struct consdev consdev_prom; 491 struct consdev consdev_kd = { 492 kdcnprobe, 493 kdcninit, 494 kdcngetc, 495 kdcnputc, 496 kdcnpollc, 497 NULL, 498 }; 499 struct consdev *cn_hw = &consdev_kd; 500 501 void 502 cons_attach_input(cc, cn) 503 struct cons_channel *cc; 504 struct consdev *cn; 505 { 506 struct kd_softc *kd = &kd_softc; 507 struct kbd_softc *kds = cc->cc_dev; 508 struct kbd_state *ks; 509 510 /* Share the keyboard state */ 511 kdcn_state = ks = &kds->k_state; 512 513 kd->kd_in = cc; 514 cc->cc_upstream = kd_cons_input; 515 516 /* Attach lower level. */ 517 cn_hw->cn_dev = cn->cn_dev; 518 cn_hw->cn_pollc = cn->cn_pollc; 519 cn_hw->cn_getc = cn->cn_getc; 520 521 /* Attach us as console. */ 522 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 523 cn_tab->cn_probe = kdcnprobe; 524 cn_tab->cn_init = kdcninit; 525 cn_tab->cn_getc = kdcngetc; 526 cn_tab->cn_pollc = kdcnpollc; 527 cn_tab->cn_pri = CN_INTERNAL; 528 529 /* Set up initial PROM input channel for /dev/console */ 530 prom_cons_channel.cc_dev = NULL; 531 prom_cons_channel.cc_iopen = kd_rom_iopen; 532 prom_cons_channel.cc_iclose = kd_rom_iclose; 533 534 /* Indicate that it is OK to use the PROM fbwrite */ 535 kd_is_console = 1; 536 } 537 538 539 void kd_attach_input(struct cons_channel *); 540 void 541 kd_attach_input(cc) 542 struct cons_channel *cc; 543 { 544 struct kd_softc *kd = &kd_softc; 545 546 kd->kd_in = cc; 547 cc->cc_upstream = kd_cons_input; 548 } 549 550 551 /* We never call this. */ 552 static void 553 kdcnprobe(cn) 554 struct consdev *cn; 555 { 556 } 557 558 static void 559 kdcninit(cn) 560 struct consdev *cn; 561 { 562 #if 0 563 struct kbd_state *ks = kdcn_state; 564 565 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 566 cn->cn_pri = CN_INTERNAL; 567 568 /* This prepares kbd_translate() */ 569 ks->kbd_id = KBD_MIN_TYPE; 570 kbd_xlate_init(ks); 571 572 /* Set up initial PROM input channel for /dev/console */ 573 prom_cons_channel.cc_dev = NULL; 574 prom_cons_channel.cc_iopen = kd_rom_iopen; 575 prom_cons_channel.cc_iclose = kd_rom_iclose; 576 cons_attach_input(&prom_cons_channel); 577 578 /* Indicate that it is OK to use the PROM fbwrite */ 579 kd_is_console = 1; 580 #endif 581 } 582 583 static int 584 kdcngetc(dev) 585 dev_t dev; 586 { 587 struct kbd_state *ks = kdcn_state; 588 int code, class, data, keysym; 589 extern int prom_cngetc __P((dev_t)); 590 591 592 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev); 593 for (;;) { 594 code = (*cn_hw->cn_getc)(dev); 595 keysym = kbd_code_to_keysym(ks, code); 596 class = KEYSYM_CLASS(keysym); 597 598 switch (class) { 599 case KEYSYM_ASCII: 600 goto out; 601 602 case KEYSYM_CLRMOD: 603 case KEYSYM_SETMOD: 604 data = (keysym & 0x1F); 605 /* Only allow ctrl or shift. */ 606 if (data > KBMOD_SHIFT_R) 607 break; 608 data = 1 << data; 609 if (class == KEYSYM_SETMOD) 610 ks->kbd_modbits |= data; 611 else 612 ks->kbd_modbits &= ~data; 613 break; 614 615 case KEYSYM_ALL_UP: 616 /* No toggle keys here. */ 617 ks->kbd_modbits = 0; 618 break; 619 620 default: /* ignore all other keysyms */ 621 break; 622 } 623 } 624 out: 625 return (keysym); 626 } 627 628 static void 629 kdcnputc(dev, c) 630 dev_t dev; 631 int c; 632 { 633 int s; 634 char c0 = (c & 0x7f); 635 636 s = splhigh(); 637 prom_write(prom_stdout(), &c0, 1); 638 splx(s); 639 } 640 641 static void 642 kdcnpollc(dev, on) 643 dev_t dev; 644 int on; 645 { 646 struct kbd_state *ks = kdcn_state; 647 648 if (on) { 649 /* Entering debugger. */ 650 #if NFB > 0 651 fb_unblank(); 652 #endif 653 /* Clear shift keys too. */ 654 ks->kbd_modbits = 0; 655 } else { 656 /* Resuming kernel. */ 657 } 658 (*cn_hw->cn_pollc)(dev, on); 659 } 660