1 /* $NetBSD: kd.c,v 1.20 1996/10/13 03:47:32 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Gordon W. Ross 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 4. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Gordon Ross 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Keyboard/Display device. 35 * 36 * This driver exists simply to provide a tty device that 37 * the indirect console driver can point to. 38 * The kbd driver sends its input here. 39 * Output goes to the screen via PROM printf. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/proc.h> 44 #include <sys/systm.h> 45 #include <sys/ioctl.h> 46 #include <sys/tty.h> 47 #include <sys/file.h> 48 #include <sys/conf.h> 49 #include <sys/device.h> 50 51 #include <machine/autoconf.h> 52 #include <machine/mon.h> 53 #include <machine/psl.h> 54 55 #include <dev/cons.h> 56 #include <dev/sun/kbd_xlate.h> 57 58 #define KDMAJOR 1 59 #define PUT_WSIZE 64 60 61 cdev_decl(kd); /* open, close, read, write, ioctl, stop, ... */ 62 63 struct kd_softc { 64 struct device kd_dev; /* required first: base device */ 65 struct tty *kd_tty; 66 }; 67 68 /* 69 * There is no point in pretending there might be 70 * more than one keyboard/display device. 71 */ 72 struct kd_softc kd_softc; 73 74 static int kdparam(struct tty *, struct termios *); 75 static void kdstart(struct tty *); 76 77 int kd_is_console; 78 79 /* 80 * This is called by kbd_attach() 81 * XXX - Make this a proper child of kbd? 82 */ 83 void 84 kd_init(unit) 85 int unit; 86 { 87 struct kd_softc *kd; 88 struct tty *tp; 89 90 if (unit != 0) 91 return; 92 kd = &kd_softc; /* XXX */ 93 94 tp = ttymalloc(); 95 tp->t_oproc = kdstart; 96 tp->t_param = kdparam; 97 tp->t_dev = makedev(KDMAJOR, unit); 98 tty_attach(tp); 99 100 kd->kd_tty = tp; 101 102 return; 103 } 104 105 struct tty * 106 kdtty(dev) 107 dev_t dev; 108 { 109 struct kd_softc *kd; 110 111 kd = &kd_softc; /* XXX */ 112 return (kd->kd_tty); 113 } 114 115 int 116 kdopen(dev, flag, mode, p) 117 dev_t dev; 118 int flag, mode; 119 struct proc *p; 120 { 121 struct kd_softc *kd; 122 int error, s, unit; 123 struct tty *tp; 124 125 unit = minor(dev); 126 if (unit != 0) 127 return ENXIO; 128 kd = &kd_softc; /* XXX */ 129 tp = kd->kd_tty; 130 131 if ((error = kbd_iopen(unit)) != 0) { 132 #ifdef DIAGNOSTIC 133 printf("kd: kbd_iopen, error=%d\n", error); 134 #endif 135 return (error); 136 } 137 138 /* It's simpler to do this up here. */ 139 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) 140 == (TS_ISOPEN | TS_XCLUDE)) 141 && (p->p_ucred->cr_uid != 0) ) 142 { 143 return (EBUSY); 144 } 145 146 s = spltty(); 147 148 if ((tp->t_state & TS_ISOPEN) == 0) { 149 /* First open. */ 150 ttychars(tp); 151 tp->t_iflag = TTYDEF_IFLAG; 152 tp->t_oflag = TTYDEF_OFLAG; 153 tp->t_cflag = TTYDEF_CFLAG; 154 tp->t_lflag = TTYDEF_LFLAG; 155 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 156 (void) kdparam(tp, &tp->t_termios); 157 ttsetwater(tp); 158 /* Flush pending input? Clear translator? */ 159 /* This (pseudo)device always has SOFTCAR */ 160 tp->t_state |= TS_CARR_ON; 161 } 162 163 splx(s); 164 165 return ((*linesw[tp->t_line].l_open)(dev, tp)); 166 } 167 168 int 169 kdclose(dev, flag, mode, p) 170 dev_t dev; 171 int flag, mode; 172 struct proc *p; 173 { 174 struct kd_softc *kd; 175 struct tty *tp; 176 177 kd = &kd_softc; /* XXX */ 178 tp = kd->kd_tty; 179 180 /* XXX This is for cons.c. */ 181 if ((tp->t_state & TS_ISOPEN) == 0) 182 return 0; 183 184 (*linesw[tp->t_line].l_close)(tp, flag); 185 ttyclose(tp); 186 return (0); 187 } 188 189 int 190 kdread(dev, uio, flag) 191 dev_t dev; 192 struct uio *uio; 193 int flag; 194 { 195 struct kd_softc *kd; 196 struct tty *tp; 197 198 kd = &kd_softc; /* XXX */ 199 tp = kd->kd_tty; 200 201 return ((*linesw[tp->t_line].l_read)(tp, uio, flag)); 202 } 203 204 int 205 kdwrite(dev, uio, flag) 206 dev_t dev; 207 struct uio *uio; 208 int flag; 209 { 210 struct kd_softc *kd; 211 struct tty *tp; 212 213 kd = &kd_softc; /* XXX */ 214 tp = kd->kd_tty; 215 216 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 217 } 218 219 int 220 kdioctl(dev, cmd, data, flag, p) 221 dev_t dev; 222 u_long cmd; 223 caddr_t data; 224 int flag; 225 struct proc *p; 226 { 227 struct kd_softc *kd; 228 struct tty *tp; 229 int error; 230 231 kd = &kd_softc; /* XXX */ 232 tp = kd->kd_tty; 233 234 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 235 if (error >= 0) 236 return error; 237 error = ttioctl(tp, cmd, data, flag, p); 238 if (error >= 0) 239 return error; 240 241 /* Handle any ioctl commands specific to kbd/display. */ 242 /* XXX - Send KB* ioctls to kbd module? */ 243 /* XXX - Send FB* ioctls to fb module? */ 244 245 return ENOTTY; 246 } 247 248 249 static int 250 kdparam(tp, t) 251 struct tty *tp; 252 struct termios *t; 253 { 254 /* XXX - These are ignored... */ 255 tp->t_ispeed = t->c_ispeed; 256 tp->t_ospeed = t->c_ospeed; 257 tp->t_cflag = t->c_cflag; 258 return 0; 259 } 260 261 262 void 263 kdstop(tp, flag) 264 struct tty *tp; 265 int flag; 266 { 267 268 } 269 270 static void kd_later(void*); 271 static void kd_putfb(struct tty *); 272 273 void 274 kdstart(tp) 275 struct tty *tp; 276 { 277 struct clist *cl; 278 register int s; 279 280 s = spltty(); 281 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT)) 282 goto out; 283 284 cl = &tp->t_outq; 285 if (cl->c_cc) { 286 if (kd_is_console) { 287 tp->t_state |= TS_BUSY; 288 if ((s & PSL_IPL) == 0) { 289 /* called at level zero - update screen now. */ 290 (void) splsoftclock(); 291 kd_putfb(tp); 292 (void) spltty(); 293 tp->t_state &= ~TS_BUSY; 294 } else { 295 /* called at interrupt level - do it later */ 296 timeout(kd_later, (void*)tp, 0); 297 } 298 } else { 299 /* 300 * This driver uses the PROM for writing the screen, 301 * and that only works if this is the console device. 302 * If this is not the console, just flush the output. 303 * Sorry. (In that case, use xdm instead of getty.) 304 */ 305 ndflush(cl, cl->c_cc); 306 } 307 } 308 if (cl->c_cc <= tp->t_lowat) { 309 if (tp->t_state & TS_ASLEEP) { 310 tp->t_state &= ~TS_ASLEEP; 311 wakeup((caddr_t)cl); 312 } 313 selwakeup(&tp->t_wsel); 314 } 315 out: 316 splx(s); 317 } 318 319 /* 320 * Timeout function to do delayed writes to the screen. 321 * Called at splsoftclock when requested by kdstart. 322 */ 323 static void 324 kd_later(tpaddr) 325 void *tpaddr; 326 { 327 struct tty *tp = tpaddr; 328 register int s; 329 330 kd_putfb(tp); 331 332 s = spltty(); 333 tp->t_state &= ~TS_BUSY; 334 (*linesw[tp->t_line].l_start)(tp); 335 splx(s); 336 } 337 338 /* 339 * Put text on the screen using the PROM monitor. 340 * This can take a while, so to avoid missing 341 * interrupts, this is called at splsoftclock. 342 */ 343 static void kd_putfb(tp) 344 struct tty *tp; 345 { 346 char buf[PUT_WSIZE]; 347 struct clist *cl = &tp->t_outq; 348 char *p, *end; 349 int len; 350 351 while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) { 352 /* PROM will barf if high bits are set. */ 353 p = buf; 354 end = buf + len; 355 while (p < end) 356 *p++ &= 0x7f; 357 (romVectorPtr->fbWriteStr)(buf, len); 358 } 359 } 360 361 /* 362 * Our "interrupt" routine for input. This is called by 363 * the keyboard driver (dev/sun/kbd.c) at spltty. 364 */ 365 void 366 kd_input(c) 367 int c; 368 { 369 struct kd_softc *kd = &kd_softc; 370 struct tty *tp; 371 372 /* XXX: Make sure the device is open. */ 373 tp = kd->kd_tty; 374 if (tp == NULL) 375 return; 376 if ((tp->t_state & TS_ISOPEN) == 0) 377 return; 378 379 (*linesw[tp->t_line].l_rint)(c, tp); 380 } 381 382 383 /**************************************************************** 384 * kd console support 385 ****************************************************************/ 386 387 extern void *zs_conschan; 388 extern int zs_getc(); 389 extern void nullcnprobe(); 390 cons_decl(kd); 391 392 /* The debugger gets its own key translation state. */ 393 static struct kbd_state kdcn_state; 394 395 void 396 kdcninit(cn) 397 struct consdev *cn; 398 { 399 struct kbd_state *ks = &kdcn_state; 400 401 mon_printf("console on kd0 (keyboard/display)\n"); 402 403 /* This prepares kbd_translate() */ 404 ks->kbd_id = KBD_MIN_TYPE; 405 kbd_xlate_init(ks); 406 407 /* Indicate that it is OK to use the PROM fbwrite */ 408 kd_is_console = 1; 409 } 410 411 int 412 kdcngetc(dev) 413 dev_t dev; 414 { 415 struct kbd_state *ks = &kdcn_state; 416 int code, class, data, keysym; 417 418 for (;;) { 419 code = zs_getc(zs_conschan); 420 keysym = kbd_code_to_keysym(ks, code); 421 class = KEYSYM_CLASS(keysym); 422 423 switch (class) { 424 case KEYSYM_ASCII: 425 goto out; 426 427 case KEYSYM_CLRMOD: 428 case KEYSYM_SETMOD: 429 data = (keysym & 0x1F); 430 /* Only allow ctrl or shift. */ 431 if (data > KBMOD_SHIFT_R) 432 break; 433 data = 1 << data; 434 if (class == KEYSYM_SETMOD) 435 ks->kbd_modbits |= data; 436 else 437 ks->kbd_modbits &= ~data; 438 break; 439 440 case KEYSYM_ALL_UP: 441 /* No toggle keys here. */ 442 ks->kbd_modbits = 0; 443 break; 444 445 default: /* ignore all other keysyms */ 446 break; 447 } 448 } 449 out: 450 return (keysym); 451 } 452 453 void 454 kdcnputc(dev, c) 455 dev_t dev; 456 int c; 457 { 458 (romVectorPtr->fbWriteChar)(c & 0x7f); 459 } 460 461 extern void fb_unblank(); 462 void kdcnpollc(dev, on) 463 dev_t dev; 464 int on; 465 { 466 struct kbd_state *ks = &kdcn_state; 467 468 if (on) { 469 /* Entering debugger. */ 470 fb_unblank(); 471 /* Clear shift keys too. */ 472 ks->kbd_modbits = 0; 473 } else { 474 /* Resuming kernel. */ 475 } 476 } 477 478 struct consdev consdev_kd = { 479 nullcnprobe, 480 kdcninit, 481 kdcngetc, 482 kdcnputc, 483 kdcnpollc, 484 makedev(KDMAJOR, 0), 485 CN_INTERNAL 486 }; 487 488