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