1 /* $NetBSD: kd.c,v 1.33 2005/02/25 19:16:45 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 * 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.33 2005/02/25 19:16:45 martin 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, p) 163 dev_t dev; 164 int flag, mode; 165 struct proc *p; 166 { 167 struct kd_softc *kd; 168 int error, s, unit; 169 struct tty *tp; 170 static int firstopen = 1; 171 172 unit = minor(dev); 173 if (unit != 0) 174 return ENXIO; 175 kd = &kd_softc; /* XXX */ 176 177 if (firstopen) { 178 kd_init(kd); 179 firstopen = 0; 180 } 181 tp = kd->kd_tty; 182 183 /* It's simpler to do this up here. */ 184 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) 185 == (TS_ISOPEN | TS_XCLUDE)) 186 && (p->p_ucred->cr_uid != 0) ) 187 { 188 return (EBUSY); 189 } 190 191 s = spltty(); 192 193 if ((tp->t_state & TS_ISOPEN) == 0) { 194 /* First open. */ 195 196 /* Notify the input device that serves us */ 197 struct cons_channel *cc = kd->kd_in; 198 if (cc != NULL && 199 (error = (*cc->cc_iopen)(cc)) != 0) { 200 splx(s); 201 return (error); 202 } 203 204 ttychars(tp); 205 tp->t_iflag = TTYDEF_IFLAG; 206 tp->t_oflag = TTYDEF_OFLAG; 207 tp->t_cflag = TTYDEF_CFLAG; 208 tp->t_lflag = TTYDEF_LFLAG; 209 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 210 (void) kdparam(tp, &tp->t_termios); 211 ttsetwater(tp); 212 tp->t_winsize.ws_row = kd->rows; 213 tp->t_winsize.ws_col = kd->cols; 214 /* Flush pending input? Clear translator? */ 215 /* This (pseudo)device always has SOFTCAR */ 216 tp->t_state |= TS_CARR_ON; 217 } 218 219 splx(s); 220 221 return ((*tp->t_linesw->l_open)(dev, tp)); 222 } 223 224 int 225 kdclose(dev, flag, mode, p) 226 dev_t dev; 227 int flag, mode; 228 struct proc *p; 229 { 230 struct kd_softc *kd; 231 struct tty *tp; 232 struct cons_channel *cc; 233 234 kd = &kd_softc; /* XXX */ 235 tp = kd->kd_tty; 236 237 /* XXX This is for cons.c. */ 238 if ((tp->t_state & TS_ISOPEN) == 0) 239 return 0; 240 241 (*tp->t_linesw->l_close)(tp, flag); 242 ttyclose(tp); 243 244 if ((cc = kd->kd_in) != NULL) 245 (void)(*cc->cc_iclose)(cc); 246 247 return (0); 248 } 249 250 int 251 kdread(dev, uio, flag) 252 dev_t dev; 253 struct uio *uio; 254 int flag; 255 { 256 struct kd_softc *kd; 257 struct tty *tp; 258 259 kd = &kd_softc; /* XXX */ 260 tp = kd->kd_tty; 261 262 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 263 } 264 265 int 266 kdwrite(dev, uio, flag) 267 dev_t dev; 268 struct uio *uio; 269 int flag; 270 { 271 struct kd_softc *kd; 272 struct tty *tp; 273 274 kd = &kd_softc; /* XXX */ 275 tp = kd->kd_tty; 276 277 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 278 } 279 280 int 281 kdpoll(dev, events, p) 282 dev_t dev; 283 int events; 284 struct proc *p; 285 { 286 struct kd_softc *kd; 287 struct tty *tp; 288 289 kd = &kd_softc; /* XXX */ 290 tp = kd->kd_tty; 291 292 return ((*tp->t_linesw->l_poll)(tp, events, p)); 293 } 294 295 int 296 kdioctl(dev, cmd, data, flag, p) 297 dev_t dev; 298 u_long cmd; 299 caddr_t data; 300 int flag; 301 struct proc *p; 302 { 303 struct kd_softc *kd; 304 struct tty *tp; 305 int error; 306 307 kd = &kd_softc; /* XXX */ 308 tp = kd->kd_tty; 309 310 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 311 if (error != EPASSTHROUGH) 312 return error; 313 314 error = ttioctl(tp, cmd, data, flag, p); 315 if (error != EPASSTHROUGH) 316 return error; 317 318 /* Handle any ioctl commands specific to kbd/display. */ 319 /* XXX - Send KB* ioctls to kbd module? */ 320 /* XXX - Send FB* ioctls to fb module? */ 321 322 return EPASSTHROUGH; 323 } 324 325 static int 326 kdparam(tp, t) 327 struct tty *tp; 328 struct termios *t; 329 { 330 /* XXX - These are ignored... */ 331 tp->t_ispeed = t->c_ispeed; 332 tp->t_ospeed = t->c_ospeed; 333 tp->t_cflag = t->c_cflag; 334 return 0; 335 } 336 337 338 static void kd_later(void*); 339 static void kd_putfb(struct tty *); 340 341 static void 342 kdstart(tp) 343 struct tty *tp; 344 { 345 struct clist *cl; 346 register int s; 347 348 s = spltty(); 349 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 350 goto out; 351 352 cl = &tp->t_outq; 353 if (cl->c_cc) { 354 if (kd_is_console) { 355 tp->t_state |= TS_BUSY; 356 if (s == 0) { 357 /* called at level zero - update screen now. */ 358 (void) spllowersoftclock(); 359 kd_putfb(tp); 360 (void) spltty(); 361 tp->t_state &= ~TS_BUSY; 362 } else { 363 /* called at interrupt level - do it later */ 364 callout_reset(&tp->t_rstrt_ch, 0, 365 kd_later, tp); 366 } 367 } else { 368 /* 369 * This driver uses the PROM for writing the screen, 370 * and that only works if this is the console device. 371 * If this is not the console, just flush the output. 372 * Sorry. (In that case, use xdm instead of getty.) 373 */ 374 ndflush(cl, cl->c_cc); 375 } 376 } 377 if (cl->c_cc <= tp->t_lowat) { 378 if (tp->t_state & TS_ASLEEP) { 379 tp->t_state &= ~TS_ASLEEP; 380 wakeup((caddr_t)cl); 381 } 382 selwakeup(&tp->t_wsel); 383 } 384 out: 385 splx(s); 386 } 387 388 /* 389 * Timeout function to do delayed writes to the screen. 390 * Called at splsoftclock when requested by kdstart. 391 */ 392 static void 393 kd_later(tpaddr) 394 void *tpaddr; 395 { 396 struct tty *tp = tpaddr; 397 register int s; 398 399 kd_putfb(tp); 400 401 s = spltty(); 402 tp->t_state &= ~TS_BUSY; 403 (*tp->t_linesw->l_start)(tp); 404 splx(s); 405 } 406 407 /* 408 * Put text on the screen using the PROM monitor. 409 * This can take a while, so to avoid missing 410 * interrupts, this is called at splsoftclock. 411 */ 412 static void 413 kd_putfb(tp) 414 struct tty *tp; 415 { 416 char buf[PUT_WSIZE]; 417 struct clist *cl = &tp->t_outq; 418 char *p, *end; 419 int len; 420 421 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 422 /* PROM will barf if high bits are set. */ 423 p = buf; 424 end = buf + len; 425 while (p < end) 426 *p++ &= 0x7f; 427 /* Now let the PROM print it. */ 428 prom_write(prom_stdout(), buf, len); 429 } 430 } 431 432 /* 433 * Default PROM-based console input stream 434 */ 435 static int kd_rom_iopen __P((struct cons_channel *)); 436 static int kd_rom_iclose __P((struct cons_channel *)); 437 438 static struct cons_channel prom_cons_channel; 439 440 int 441 kd_rom_iopen(cc) 442 struct cons_channel *cc; 443 { 444 return (0); 445 } 446 447 int 448 kd_rom_iclose(cc) 449 struct cons_channel *cc; 450 { 451 return (0); 452 } 453 454 /* 455 * Our "interrupt" routine for input. This is called by 456 * the keyboard driver (dev/sun/kbd.c) at spltty. 457 */ 458 void 459 kd_cons_input(c) 460 int c; 461 { 462 struct kd_softc *kd = &kd_softc; 463 struct tty *tp; 464 465 /* XXX: Make sure the device is open. */ 466 tp = kd->kd_tty; 467 if (tp == NULL) 468 return; 469 if ((tp->t_state & TS_ISOPEN) == 0) 470 return; 471 472 (*tp->t_linesw->l_rint)(c, tp); 473 } 474 475 476 /**************************************************************** 477 * kd console support 478 ****************************************************************/ 479 480 /* The debugger gets its own key translation state. */ 481 static struct kbd_state *kdcn_state; 482 483 static void kdcnprobe __P((struct consdev *)); 484 static void kdcninit __P((struct consdev *)); 485 static void kdcnputc __P((dev_t, int)); 486 static void kdcnpollc __P((dev_t, int)); 487 488 /* The keyboard driver uses cn_hw to access the real console driver */ 489 extern struct consdev consdev_prom; 490 struct consdev consdev_kd = { 491 kdcnprobe, 492 kdcninit, 493 kdcngetc, 494 kdcnputc, 495 kdcnpollc, 496 NULL, 497 }; 498 struct consdev *cn_hw = &consdev_kd; 499 500 void 501 cons_attach_input(cc, cn) 502 struct cons_channel *cc; 503 struct consdev *cn; 504 { 505 struct kd_softc *kd = &kd_softc; 506 struct kbd_softc *kds = cc->cc_dev; 507 struct kbd_state *ks; 508 509 /* Share the keyboard state */ 510 kdcn_state = ks = &kds->k_state; 511 512 kd->kd_in = cc; 513 cc->cc_upstream = kd_cons_input; 514 515 /* Attach lower level. */ 516 cn_hw->cn_dev = cn->cn_dev; 517 cn_hw->cn_pollc = cn->cn_pollc; 518 cn_hw->cn_getc = cn->cn_getc; 519 520 /* Attach us as console. */ 521 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 522 cn_tab->cn_probe = kdcnprobe; 523 cn_tab->cn_init = kdcninit; 524 cn_tab->cn_getc = kdcngetc; 525 cn_tab->cn_pollc = kdcnpollc; 526 cn_tab->cn_pri = CN_INTERNAL; 527 528 /* Set up initial PROM input channel for /dev/console */ 529 prom_cons_channel.cc_dev = NULL; 530 prom_cons_channel.cc_iopen = kd_rom_iopen; 531 prom_cons_channel.cc_iclose = kd_rom_iclose; 532 533 /* Indicate that it is OK to use the PROM fbwrite */ 534 kd_is_console = 1; 535 } 536 537 538 void kd_attach_input(struct cons_channel *); 539 void 540 kd_attach_input(cc) 541 struct cons_channel *cc; 542 { 543 struct kd_softc *kd = &kd_softc; 544 545 kd->kd_in = cc; 546 cc->cc_upstream = kd_cons_input; 547 } 548 549 550 /* We never call this. */ 551 static void 552 kdcnprobe(cn) 553 struct consdev *cn; 554 { 555 } 556 557 static void 558 kdcninit(cn) 559 struct consdev *cn; 560 { 561 #if 0 562 struct kbd_state *ks = kdcn_state; 563 564 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 565 cn->cn_pri = CN_INTERNAL; 566 567 /* This prepares kbd_translate() */ 568 ks->kbd_id = KBD_MIN_TYPE; 569 kbd_xlate_init(ks); 570 571 /* Set up initial PROM input channel for /dev/console */ 572 prom_cons_channel.cc_dev = NULL; 573 prom_cons_channel.cc_iopen = kd_rom_iopen; 574 prom_cons_channel.cc_iclose = kd_rom_iclose; 575 cons_attach_input(&prom_cons_channel); 576 577 /* Indicate that it is OK to use the PROM fbwrite */ 578 kd_is_console = 1; 579 #endif 580 } 581 582 static int 583 kdcngetc(dev) 584 dev_t dev; 585 { 586 struct kbd_state *ks = kdcn_state; 587 int code, class, data, keysym; 588 extern int prom_cngetc __P((dev_t)); 589 590 591 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev); 592 for (;;) { 593 code = (*cn_hw->cn_getc)(dev); 594 keysym = kbd_code_to_keysym(ks, code); 595 class = KEYSYM_CLASS(keysym); 596 597 switch (class) { 598 case KEYSYM_ASCII: 599 goto out; 600 601 case KEYSYM_CLRMOD: 602 case KEYSYM_SETMOD: 603 data = (keysym & 0x1F); 604 /* Only allow ctrl or shift. */ 605 if (data > KBMOD_SHIFT_R) 606 break; 607 data = 1 << data; 608 if (class == KEYSYM_SETMOD) 609 ks->kbd_modbits |= data; 610 else 611 ks->kbd_modbits &= ~data; 612 break; 613 614 case KEYSYM_ALL_UP: 615 /* No toggle keys here. */ 616 ks->kbd_modbits = 0; 617 break; 618 619 default: /* ignore all other keysyms */ 620 break; 621 } 622 } 623 out: 624 return (keysym); 625 } 626 627 static void 628 kdcnputc(dev, c) 629 dev_t dev; 630 int c; 631 { 632 int s; 633 char c0 = (c & 0x7f); 634 635 s = splhigh(); 636 prom_write(prom_stdout(), &c0, 1); 637 splx(s); 638 } 639 640 static void 641 kdcnpollc(dev, on) 642 dev_t dev; 643 int on; 644 { 645 struct kbd_state *ks = kdcn_state; 646 647 if (on) { 648 /* Entering debugger. */ 649 #if NFB > 0 650 fb_unblank(); 651 #endif 652 /* Clear shift keys too. */ 653 ks->kbd_modbits = 0; 654 } else { 655 /* Resuming kernel. */ 656 } 657 (*cn_hw->cn_pollc)(dev, on); 658 } 659