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