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