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