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