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