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