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