Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.31
      1 /*	$NetBSD: kd.c,v 1.31 2004/03/16 22:47:10 pk 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  * Console driver based on PROM primitives.
     41  *
     42  * This driver exists to provide a tty device that the indirect
     43  * console driver can point to. It also provides a hook that
     44  * allows another device to serve console input. This will normally
     45  * be a keyboard driver (see sys/dev/sun/kbd.c)
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: kd.c,v 1.31 2004/03/16 22:47:10 pk Exp $");
     50 
     51 #include "opt_kgdb.h"
     52 #include "fb.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/proc.h>
     56 #include <sys/systm.h>
     57 #include <sys/kernel.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/tty.h>
     60 #include <sys/file.h>
     61 #include <sys/conf.h>
     62 #include <sys/device.h>
     63 
     64 #include <machine/bsd_openprom.h>
     65 #include <machine/promlib.h>
     66 #include <machine/eeprom.h>
     67 #include <machine/psl.h>
     68 #include <machine/cpu.h>
     69 #include <machine/kbd.h>
     70 #include <machine/autoconf.h>
     71 
     72 #if defined(RASTERCONSOLE) && NFB > 0
     73 #include <dev/sun/fbio.h>
     74 #include <dev/sun/fbvar.h>
     75 #endif
     76 
     77 #include <dev/cons.h>
     78 #include <sparc/dev/cons.h>
     79 
     80 #include <dev/sun/event_var.h>
     81 #include <dev/sun/kbd_xlate.h>
     82 #include <dev/sun/kbdvar.h>
     83 
     84 #define PUT_WSIZE	64
     85 
     86 struct kd_softc {
     87 	struct	device kd_dev;		/* required first: base device */
     88 	struct  tty *kd_tty;
     89 	int rows, cols;
     90 
     91 	/* Console input hook */
     92 	struct cons_channel *kd_in;
     93 };
     94 
     95 /*
     96  * There is no point in pretending there might be
     97  * more than one keyboard/display device.
     98  */
     99 static struct kd_softc kd_softc;
    100 
    101 static int kdparam(struct tty *, struct termios *);
    102 static void kdstart(struct tty *);
    103 static void kd_init(struct kd_softc *);
    104 static void kd_cons_input(int);
    105 
    106 dev_type_open(kdopen);
    107 dev_type_close(kdclose);
    108 dev_type_read(kdread);
    109 dev_type_write(kdwrite);
    110 dev_type_ioctl(kdioctl);
    111 dev_type_tty(kdtty);
    112 dev_type_poll(kdpoll);
    113 
    114 const struct cdevsw kd_cdevsw = {
    115 	kdopen, kdclose, kdread, kdwrite, kdioctl,
    116 	nostop, kdtty, kdpoll, nommap, ttykqfilter, D_TTY
    117 };
    118 
    119 /*
    120  * Prepare the console tty; called on first open of /dev/console
    121  */
    122 void
    123 kd_init(kd)
    124 	struct kd_softc *kd;
    125 {
    126 	struct tty *tp;
    127 
    128 	tp = ttymalloc();
    129 	tp->t_oproc = kdstart;
    130 	tp->t_param = kdparam;
    131 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    132 
    133 	tty_attach(tp);
    134 	kd->kd_tty = tp;
    135 
    136 	/*
    137 	 * Get the console struct winsize.
    138 	 */
    139 #if defined(RASTERCONSOLE) && NFB > 0
    140 	/* If the raster console driver is attached, copy its size */
    141 	kd->rows = fbrcons_rows();
    142 	kd->cols = fbrcons_cols();
    143 	rcons_ttyinit(tp);
    144 #endif
    145 
    146 	/* else, consult the PROM */
    147 	switch (prom_version()) {
    148 	char prop[6+1];		/* Enough for six digits */
    149 	struct eeprom *ep;
    150 	case PROM_OLDMON:
    151 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
    152 			break;
    153 		if (kd->rows == 0)
    154 			kd->rows = (u_short)ep->eeTtyRows;
    155 		if (kd->cols == 0)
    156 			kd->cols = (u_short)ep->eeTtyCols;
    157 		break;
    158 
    159 	case PROM_OBP_V0:
    160 	case PROM_OBP_V2:
    161 	case PROM_OBP_V3:
    162 	case PROM_OPENFIRM:
    163 		if (kd->rows == 0 &&
    164 		    prom_getoption("screen-#rows", prop, sizeof prop) == 0)
    165 			kd->rows = strtoul(prop, NULL, 10);
    166 
    167 		if (kd->cols == 0 &&
    168 		    prom_getoption("screen-#columns", prop, sizeof prop) == 0)
    169 			kd->cols = strtoul(prop, NULL, 10);
    170 
    171 		break;
    172 	}
    173 
    174 	return;
    175 }
    176 
    177 struct tty *
    178 kdtty(dev)
    179 	dev_t dev;
    180 {
    181 	struct kd_softc *kd;
    182 
    183 	kd = &kd_softc; 	/* XXX */
    184 	return (kd->kd_tty);
    185 }
    186 
    187 int
    188 kdopen(dev, flag, mode, p)
    189 	dev_t dev;
    190 	int flag, mode;
    191 	struct proc *p;
    192 {
    193 	struct kd_softc *kd;
    194 	int error, s, unit;
    195 	struct tty *tp;
    196 static	int firstopen = 1;
    197 
    198 	unit = minor(dev);
    199 	if (unit != 0)
    200 		return ENXIO;
    201 
    202 	kd = &kd_softc; 	/* XXX */
    203 	if (firstopen) {
    204 		kd_init(kd);
    205 		firstopen = 0;
    206 	}
    207 
    208 	tp = kd->kd_tty;
    209 
    210 	/* It's simpler to do this up here. */
    211 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    212 	     ==             (TS_ISOPEN | TS_XCLUDE))
    213 	    && (p->p_ucred->cr_uid != 0) )
    214 	{
    215 		return (EBUSY);
    216 	}
    217 
    218 	s = spltty();
    219 	if ((tp->t_state & TS_ISOPEN) == 0) {
    220 		/* First open. */
    221 
    222 		/* Notify the input device that serves us */
    223 		struct cons_channel *cc = kd->kd_in;
    224 		if (cc != NULL &&
    225 		    (error = (*cc->cc_iopen)(cc)) != 0) {
    226 			return (error);
    227 		}
    228 
    229 		ttychars(tp);
    230 		tp->t_iflag = TTYDEF_IFLAG;
    231 		tp->t_oflag = TTYDEF_OFLAG;
    232 		tp->t_cflag = TTYDEF_CFLAG;
    233 		tp->t_lflag = TTYDEF_LFLAG;
    234 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    235 		(void) kdparam(tp, &tp->t_termios);
    236 		ttsetwater(tp);
    237 		tp->t_winsize.ws_row = kd->rows;
    238 		tp->t_winsize.ws_col = kd->cols;
    239 		/* Flush pending input?  Clear translator? */
    240 		/* This (pseudo)device always has SOFTCAR */
    241 		tp->t_state |= TS_CARR_ON;
    242 	}
    243 
    244 	splx(s);
    245 
    246 	return ((*tp->t_linesw->l_open)(dev, tp));
    247 }
    248 
    249 int
    250 kdclose(dev, flag, mode, p)
    251 	dev_t dev;
    252 	int flag, mode;
    253 	struct proc *p;
    254 {
    255 	struct kd_softc *kd;
    256 	struct tty *tp;
    257 	struct cons_channel *cc;
    258 
    259 	kd = &kd_softc; 	/* XXX */
    260 	tp = kd->kd_tty;
    261 
    262 	/* XXX This is for cons.c. */
    263 	if ((tp->t_state & TS_ISOPEN) == 0)
    264 		return 0;
    265 
    266 	(*tp->t_linesw->l_close)(tp, flag);
    267 	ttyclose(tp);
    268 
    269 	if ((cc = kd->kd_in) != NULL)
    270 		(void)(*cc->cc_iclose)(cc);
    271 
    272 	return (0);
    273 }
    274 
    275 int
    276 kdread(dev, uio, flag)
    277 	dev_t dev;
    278 	struct uio *uio;
    279 	int flag;
    280 {
    281 	struct kd_softc *kd;
    282 	struct tty *tp;
    283 
    284 	kd = &kd_softc; 	/* XXX */
    285 	tp = kd->kd_tty;
    286 
    287 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    288 }
    289 
    290 int
    291 kdwrite(dev, uio, flag)
    292 	dev_t dev;
    293 	struct uio *uio;
    294 	int flag;
    295 {
    296 	struct kd_softc *kd;
    297 	struct tty *tp;
    298 
    299 	kd = &kd_softc; 	/* XXX */
    300 	tp = kd->kd_tty;
    301 
    302 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    303 }
    304 
    305 int
    306 kdpoll(dev, events, p)
    307 	dev_t dev;
    308 	int events;
    309 	struct proc *p;
    310 {
    311 	struct kd_softc *kd;
    312 	struct tty *tp;
    313 
    314 	kd = &kd_softc; 	/* XXX */
    315 	tp = kd->kd_tty;
    316 
    317 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    318 }
    319 
    320 int
    321 kdioctl(dev, cmd, data, flag, p)
    322 	dev_t dev;
    323 	u_long cmd;
    324 	caddr_t data;
    325 	int flag;
    326 	struct proc *p;
    327 {
    328 	struct kd_softc *kd;
    329 	struct tty *tp;
    330 	int error;
    331 
    332 	kd = &kd_softc; 	/* XXX */
    333 	tp = kd->kd_tty;
    334 
    335 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    336 	if (error != EPASSTHROUGH)
    337 		return error;
    338 
    339 	error = ttioctl(tp, cmd, data, flag, p);
    340 	if (error != EPASSTHROUGH)
    341 		return error;
    342 
    343 	/* Handle any ioctl commands specific to kbd/display. */
    344 	/* XXX - Send KB* ioctls to kbd module? */
    345 	/* XXX - Send FB* ioctls to fb module?  */
    346 
    347 	return EPASSTHROUGH;
    348 }
    349 
    350 static int
    351 kdparam(tp, t)
    352 	struct tty *tp;
    353 	struct termios *t;
    354 {
    355 	/* XXX - These are ignored... */
    356 	tp->t_ispeed = t->c_ispeed;
    357 	tp->t_ospeed = t->c_ospeed;
    358 	tp->t_cflag = t->c_cflag;
    359 	return 0;
    360 }
    361 
    362 
    363 static void kd_later(void*);
    364 static void kd_putfb(struct tty *);
    365 
    366 static void
    367 kdstart(tp)
    368 	struct tty *tp;
    369 {
    370 	struct clist *cl;
    371 	int s;
    372 
    373 	s = spltty();
    374 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    375 		goto out;
    376 
    377 	cl = &tp->t_outq;
    378 	if (cl->c_cc) {
    379 		tp->t_state |= TS_BUSY;
    380 		if ((s & PSR_PIL) == 0) {
    381 			/* called at level zero - update screen now. */
    382 			(void) spllowersoftclock();
    383 			kd_putfb(tp);
    384 			(void) spltty();
    385 			tp->t_state &= ~TS_BUSY;
    386 		} else {
    387 			/* called at interrupt level - do it later */
    388 			callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp);
    389 		}
    390 	}
    391 	if (cl->c_cc <= tp->t_lowat) {
    392 		if (tp->t_state & TS_ASLEEP) {
    393 			tp->t_state &= ~TS_ASLEEP;
    394 			wakeup((caddr_t)cl);
    395 		}
    396 		selwakeup(&tp->t_wsel);
    397 	}
    398 out:
    399 	splx(s);
    400 }
    401 
    402 /*
    403  * Timeout function to do delayed writes to the screen.
    404  * Called at splsoftclock when requested by kdstart.
    405  */
    406 static void
    407 kd_later(arg)
    408 	void *arg;
    409 {
    410 	struct tty *tp = arg;
    411 	int s;
    412 
    413 	kd_putfb(tp);
    414 
    415 	s = spltty();
    416 	tp->t_state &= ~TS_BUSY;
    417 	(*tp->t_linesw->l_start)(tp);
    418 	splx(s);
    419 }
    420 
    421 /*
    422  * Put text on the screen using the PROM monitor.
    423  * This can take a while, so to avoid missing
    424  * interrupts, this is called at splsoftclock.
    425  */
    426 static void
    427 kd_putfb(tp)
    428 	struct tty *tp;
    429 {
    430 	char buf[PUT_WSIZE];
    431 	struct clist *cl = &tp->t_outq;
    432 	char *p, *end;
    433 	int len;
    434 
    435 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    436 		/* PROM will barf if high bits are set. */
    437 		p = buf;
    438 		end = buf + len;
    439 		while (p < end)
    440 			*p++ &= 0x7f;
    441 
    442 		/* Now let the PROM print it. */
    443 		prom_putstr(buf, len);
    444 	}
    445 }
    446 
    447 /*
    448  * Our "interrupt" routine for input. This is called by
    449  * the keyboard driver (dev/sun/kbd.c) at spltty.
    450  */
    451 void
    452 kd_cons_input(c)
    453 	int c;
    454 {
    455 	struct kd_softc *kd = &kd_softc;
    456 	struct tty *tp;
    457 
    458 	/* XXX: Make sure the device is open. */
    459 	tp = kd->kd_tty;
    460 	if (tp == NULL)
    461 		return;
    462 	if ((tp->t_state & TS_ISOPEN) == 0)
    463 		return;
    464 
    465 	(*tp->t_linesw->l_rint)(c, tp);
    466 }
    467 
    468 void
    469 cons_attach_input(cc, cn)
    470 	struct cons_channel *cc;
    471 	struct consdev *cn;
    472 {
    473 	struct kd_softc *kd = &kd_softc;
    474 
    475 	kd->kd_in = cc;
    476 	cc->cc_upstream = kd_cons_input;
    477 }
    478 
    479 void kd_attach_input(struct cons_channel *);
    480 void
    481 kd_attach_input(cc)
    482 	struct cons_channel *cc;
    483 {
    484 	struct kd_softc *kd = &kd_softc;
    485 
    486 	kd->kd_in = cc;
    487 	cc->cc_upstream = kd_cons_input;
    488 }
    489 
    490 /*
    491  * Default PROM-based console input stream
    492  * Since the PROM does not notify us when data is available on the
    493  * input channel these functions periodically poll the PROM.
    494  */
    495 static int kd_rom_iopen(struct cons_channel *);
    496 static int kd_rom_iclose(struct cons_channel *);
    497 static void kd_rom_intr(void *);
    498 
    499 static struct cons_channel prom_cons_channel = {
    500 	NULL,			/* no private data */
    501 	kd_rom_iopen,
    502 	kd_rom_iclose,
    503 	NULL			/* will be set by kd driver */
    504 };
    505 
    506 static struct callout prom_cons_callout = CALLOUT_INITIALIZER;
    507 
    508 int
    509 kd_rom_iopen(cc)
    510 	struct cons_channel *cc;
    511 {
    512 
    513 	/* Poll for ROM input 4 times per second */
    514 	callout_reset(&prom_cons_callout, hz / 4, kd_rom_intr, cc);
    515 	return (0);
    516 }
    517 
    518 int
    519 kd_rom_iclose(cc)
    520 	struct cons_channel *cc;
    521 {
    522 
    523 	callout_stop(&prom_cons_callout);
    524 	return (0);
    525 }
    526 
    527 /*
    528  * "Interrupt" routine for input through ROM vectors
    529  */
    530 void
    531 kd_rom_intr(arg)
    532 	void *arg;
    533 {
    534 	struct cons_channel *cc = arg;
    535 	int s, c;
    536 
    537 	callout_schedule(&prom_cons_callout, hz / 4);
    538 
    539 	s = spltty();
    540 
    541 	while ((c = prom_peekchar()) >= 0)
    542 		(*cc->cc_upstream)(c);
    543 
    544 	splx(s);
    545 }
    546 
    547 /*****************************************************************/
    548 
    549 int prom_stdin_node;
    550 int prom_stdout_node;
    551 char prom_stdin_args[16];
    552 char prom_stdout_args[16];
    553 
    554 extern void prom_cnprobe(struct consdev *);
    555 static void prom_cninit(struct consdev *);
    556 static int  prom_cngetc(dev_t);
    557 static void prom_cnputc(dev_t, int);
    558 extern void prom_cnpollc(dev_t, int);
    559 
    560 /*
    561  * The console is set to this one initially,
    562  * which lets us use the PROM until consinit()
    563  * is called to select a real console.
    564  */
    565 struct consdev consdev_prom = {
    566 	prom_cnprobe,
    567 	prom_cninit,
    568 	prom_cngetc,
    569 	prom_cnputc,
    570 	prom_cnpollc,
    571 	NULL,
    572 };
    573 
    574 /*
    575  * The console table pointer is statically initialized
    576  * to point to the PROM table, so that early calls to printf will work.
    577  */
    578 struct consdev *cn_tab = &consdev_prom;
    579 
    580 void
    581 prom_cnprobe(cn)
    582 	struct consdev *cn;
    583 {
    584 }
    585 
    586 static void
    587 prom_cninit(cn)
    588 	struct consdev *cn;
    589 {
    590 }
    591 
    592 void
    593 prom_cnpollc(dev, on)
    594 	dev_t dev;
    595 	int on;
    596 {
    597 
    598 	if (on) {
    599 		/* Entering debugger. */
    600 #if NFB > 0
    601 		fb_unblank();
    602 #endif
    603 	} else {
    604 		/* Resuming kernel. */
    605 	}
    606 }
    607 
    608 
    609 /*
    610  * PROM console input putchar.
    611  */
    612 static int
    613 prom_cngetc(dev)
    614 	dev_t dev;
    615 {
    616 	int s, c;
    617 
    618 	s = splhigh();
    619 	c = prom_getchar();
    620 	splx(s);
    621 	return (c);
    622 }
    623 
    624 /*
    625  * PROM console output putchar.
    626  */
    627 static void
    628 prom_cnputc(dev, c)
    629 	dev_t dev;
    630 	int c;
    631 {
    632 
    633 	prom_putchar(c);
    634 }
    635 
    636 
    637 /*****************************************************************/
    638 
    639 static void prom_get_device_args(const char *, char *, unsigned int);
    640 
    641 void
    642 prom_get_device_args(prop, args, sz)
    643 	const char *prop;
    644 	char *args;
    645 	unsigned int sz;
    646 {
    647 	char *cp, buffer[128];
    648 
    649 	cp = PROM_getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer);
    650 
    651 	/*
    652 	 * Extract device-specific arguments from a PROM device path (if any)
    653 	 */
    654 	cp = buffer + strlen(buffer);
    655 	while (cp >= buffer) {
    656 		if (*cp == ':') {
    657 			strncpy(args, cp+1, sz);
    658 			break;
    659 		}
    660 		cp--;
    661 	}
    662 }
    663 
    664 /*
    665  *
    666  */
    667 void
    668 consinit()
    669 {
    670 	int inSource, outSink;
    671 
    672 	switch (prom_version()) {
    673 	case PROM_OLDMON:
    674 	case PROM_OBP_V0:
    675 		/* The stdio handles identify the device type */
    676 		inSource = prom_stdin();
    677 		outSink  = prom_stdout();
    678 		break;
    679 
    680 	case PROM_OBP_V2:
    681 	case PROM_OBP_V3:
    682 	case PROM_OPENFIRM:
    683 
    684 		/* Save PROM arguments for device matching */
    685 		prom_get_device_args("stdin-path", prom_stdin_args,
    686 				     sizeof(prom_stdin_args));
    687 		prom_get_device_args("stdout-path", prom_stdout_args,
    688 				    sizeof(prom_stdout_args));
    689 
    690 		/*
    691 		 * Translate the STDIO package instance (`ihandle') -- that
    692 		 * the PROM has already opened for us -- to a device tree
    693 		 * node (i.e. a `phandle').
    694 		 */
    695 
    696 		prom_stdin_node = prom_instance_to_package(prom_stdin());
    697 		if (prom_stdin_node == 0)
    698 			printf("consinit: cannot convert stdin ihandle\n");
    699 
    700 		prom_stdout_node = prom_instance_to_package(prom_stdout());
    701 		if (prom_stdout_node == 0) {
    702 			printf("consinit: cannot convert stdout ihandle\n");
    703 			break;
    704 		}
    705 
    706 		break;
    707 
    708 	default:
    709 		break;
    710 	}
    711 
    712 	/* Wire up /dev/console */
    713 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    714 	cn_tab->cn_pri = CN_INTERNAL;
    715 
    716 	/* Set up initial PROM input channel for /dev/console */
    717 	cons_attach_input(&prom_cons_channel, cn_tab);
    718 
    719 #ifdef	KGDB
    720 	zs_kgdb_init();	/* XXX */
    721 #endif
    722 }
    723