1 /* $NetBSD: kd.c,v 1.47 2007/11/19 18:51:43 ad 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.47 2007/11/19 18:51:43 ad 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 ttypull(tp); 349 out: 350 splx(s2); 351 splx(s1); 352 } 353 354 /* 355 * Timeout function to do delayed writes to the screen. 356 * Called at splsoftclock when requested by kdstart. 357 */ 358 static void 359 kd_later(void *tpaddr) 360 { 361 struct tty *tp = tpaddr; 362 register int s; 363 364 kd_putfb(tp); 365 366 s = spltty(); 367 tp->t_state &= ~TS_BUSY; 368 (*tp->t_linesw->l_start)(tp); 369 splx(s); 370 } 371 372 /* 373 * Put text on the screen using the PROM monitor. 374 * This can take a while, so to avoid missing 375 * interrupts, this is called at splsoftclock. 376 */ 377 static void 378 kd_putfb(struct tty *tp) 379 { 380 char buf[PUT_WSIZE]; 381 struct clist *cl = &tp->t_outq; 382 char *p, *end; 383 int len; 384 385 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 386 /* PROM will barf if high bits are set. */ 387 p = buf; 388 end = buf + len; 389 while (p < end) 390 *p++ &= 0x7f; 391 /* Now let the PROM print it. */ 392 prom_write(prom_stdout(), buf, len); 393 } 394 } 395 396 /* 397 * Default PROM-based console input stream 398 */ 399 static int kd_rom_iopen(struct cons_channel *); 400 static int kd_rom_iclose(struct cons_channel *); 401 402 static struct cons_channel prom_cons_channel; 403 404 int 405 kd_rom_iopen(struct cons_channel *cc) 406 { 407 return (0); 408 } 409 410 int 411 kd_rom_iclose(struct cons_channel *cc) 412 { 413 return (0); 414 } 415 416 /* 417 * Our "interrupt" routine for input. This is called by 418 * the keyboard driver (dev/sun/kbd.c) at spltty. 419 */ 420 void 421 kd_cons_input(int c) 422 { 423 struct kd_softc *kd = &kd_softc; 424 struct tty *tp; 425 426 /* XXX: Make sure the device is open. */ 427 tp = kd->kd_tty; 428 if (tp == NULL) 429 return; 430 if ((tp->t_state & TS_ISOPEN) == 0) 431 return; 432 433 (*tp->t_linesw->l_rint)(c, tp); 434 } 435 436 437 /**************************************************************** 438 * kd console support 439 ****************************************************************/ 440 441 /* The debugger gets its own key translation state. */ 442 static struct kbd_state *kdcn_state; 443 444 static void kdcnprobe(struct consdev *); 445 static void kdcninit(struct consdev *); 446 static void kdcnputc(dev_t, int); 447 static void kdcnpollc(dev_t, int); 448 449 /* The keyboard driver uses cn_hw to access the real console driver */ 450 extern struct consdev consdev_prom; 451 struct consdev consdev_kd = { 452 .cn_probe = kdcnprobe, 453 .cn_init = kdcninit, 454 .cn_getc = kdcngetc, 455 .cn_putc = kdcnputc, 456 .cn_pollc = kdcnpollc, 457 }; 458 struct consdev *cn_hw = &consdev_kd; 459 460 void 461 cons_attach_input(struct cons_channel *cc, struct consdev *cn) 462 { 463 struct kd_softc *kd = &kd_softc; 464 struct kbd_softc *kds = cc->cc_dev; 465 struct kbd_state *ks; 466 467 /* Share the keyboard state */ 468 kdcn_state = ks = &kds->k_state; 469 470 kd->kd_in = cc; 471 cc->cc_upstream = kd_cons_input; 472 473 /* Attach lower level. */ 474 cn_hw->cn_dev = cn->cn_dev; 475 cn_hw->cn_pollc = cn->cn_pollc; 476 cn_hw->cn_getc = cn->cn_getc; 477 478 /* Attach us as console. */ 479 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 480 cn_tab->cn_probe = kdcnprobe; 481 cn_tab->cn_init = kdcninit; 482 cn_tab->cn_getc = kdcngetc; 483 cn_tab->cn_pollc = kdcnpollc; 484 cn_tab->cn_pri = CN_INTERNAL; 485 486 /* Set up initial PROM input channel for /dev/console */ 487 prom_cons_channel.cc_dev = NULL; 488 prom_cons_channel.cc_iopen = kd_rom_iopen; 489 prom_cons_channel.cc_iclose = kd_rom_iclose; 490 491 /* Indicate that it is OK to use the PROM fbwrite */ 492 kd_is_console = 1; 493 } 494 495 496 void kd_attach_input(struct cons_channel *); 497 void 498 kd_attach_input(struct cons_channel *cc) 499 { 500 struct kd_softc *kd = &kd_softc; 501 502 kd->kd_in = cc; 503 cc->cc_upstream = kd_cons_input; 504 } 505 506 507 /* We never call this. */ 508 static void 509 kdcnprobe(struct consdev *cn) 510 { 511 } 512 513 static void 514 kdcninit(struct consdev *cn) 515 { 516 #if 0 517 struct kbd_state *ks = kdcn_state; 518 519 cn->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0); 520 cn->cn_pri = CN_INTERNAL; 521 522 /* This prepares kbd_translate() */ 523 ks->kbd_id = KBD_MIN_TYPE; 524 kbd_xlate_init(ks); 525 526 /* Set up initial PROM input channel for /dev/console */ 527 prom_cons_channel.cc_dev = NULL; 528 prom_cons_channel.cc_iopen = kd_rom_iopen; 529 prom_cons_channel.cc_iclose = kd_rom_iclose; 530 cons_attach_input(&prom_cons_channel); 531 532 /* Indicate that it is OK to use the PROM fbwrite */ 533 kd_is_console = 1; 534 #endif 535 } 536 537 static int 538 kdcngetc(dev_t dev) 539 { 540 struct kbd_state *ks = kdcn_state; 541 int code, class, data, keysym; 542 extern int prom_cngetc(dev_t); 543 544 545 if (cn_hw->cn_getc == prom_cngetc) return (*cn_hw->cn_getc)(dev); 546 for (;;) { 547 code = (*cn_hw->cn_getc)(dev); 548 keysym = kbd_code_to_keysym(ks, code); 549 class = KEYSYM_CLASS(keysym); 550 551 switch (class) { 552 case KEYSYM_ASCII: 553 goto out; 554 555 case KEYSYM_CLRMOD: 556 case KEYSYM_SETMOD: 557 data = (keysym & 0x1F); 558 /* Only allow ctrl or shift. */ 559 if (data > KBMOD_SHIFT_R) 560 break; 561 data = 1 << data; 562 if (class == KEYSYM_SETMOD) 563 ks->kbd_modbits |= data; 564 else 565 ks->kbd_modbits &= ~data; 566 break; 567 568 case KEYSYM_ALL_UP: 569 /* No toggle keys here. */ 570 ks->kbd_modbits = 0; 571 break; 572 573 default: /* ignore all other keysyms */ 574 break; 575 } 576 } 577 out: 578 return (keysym); 579 } 580 581 static void 582 kdcnputc(dev_t dev, int c) 583 { 584 int s; 585 char c0 = (c & 0x7f); 586 587 s = splhigh(); 588 prom_write(prom_stdout(), &c0, 1); 589 splx(s); 590 } 591 592 static void 593 kdcnpollc(dev_t dev, int on) 594 { 595 struct kbd_state *ks = kdcn_state; 596 597 if (on) { 598 /* Entering debugger. */ 599 #if NFB > 0 600 fb_unblank(); 601 #endif 602 /* Clear shift keys too. */ 603 ks->kbd_modbits = 0; 604 } else { 605 /* Resuming kernel. */ 606 } 607 (*cn_hw->cn_pollc)(dev, on); 608 } 609