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