Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.8.4.3
      1  1.8.4.3  nathanw /*	$NetBSD: pcons.c,v 1.8.4.3 2002/09/17 21:17:58 nathanw Exp $	*/
      2  1.8.4.2  nathanw 
      3  1.8.4.2  nathanw /*-
      4  1.8.4.2  nathanw  * Copyright (c) 2000 Eduardo E. Horvath
      5  1.8.4.2  nathanw  * All rights reserved.
      6  1.8.4.2  nathanw  *
      7  1.8.4.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.8.4.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.8.4.2  nathanw  * are met:
     10  1.8.4.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.8.4.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.8.4.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.8.4.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.8.4.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.8.4.2  nathanw  * 3. The name of the author may not be used to endorse or promote products
     16  1.8.4.2  nathanw  *    derived from this software without specific prior written permission.
     17  1.8.4.2  nathanw  *
     18  1.8.4.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.8.4.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.8.4.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.8.4.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.8.4.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  1.8.4.2  nathanw  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  1.8.4.2  nathanw  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  1.8.4.2  nathanw  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  1.8.4.2  nathanw  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.8.4.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.8.4.2  nathanw  * SUCH DAMAGE.
     29  1.8.4.2  nathanw  */
     30  1.8.4.2  nathanw 
     31  1.8.4.2  nathanw /*
     32  1.8.4.2  nathanw  * Default console driver.  Uses the PROM or whatever
     33  1.8.4.2  nathanw  * driver(s) are appropriate.
     34  1.8.4.2  nathanw  */
     35  1.8.4.2  nathanw 
     36  1.8.4.2  nathanw #include "opt_ddb.h"
     37  1.8.4.2  nathanw 
     38  1.8.4.2  nathanw #include <sys/param.h>
     39  1.8.4.2  nathanw #include <sys/systm.h>
     40  1.8.4.2  nathanw #include <sys/conf.h>
     41  1.8.4.2  nathanw #include <sys/device.h>
     42  1.8.4.2  nathanw #include <sys/file.h>
     43  1.8.4.2  nathanw #include <sys/ioctl.h>
     44  1.8.4.2  nathanw #include <sys/kernel.h>
     45  1.8.4.2  nathanw #include <sys/proc.h>
     46  1.8.4.2  nathanw #include <sys/tty.h>
     47  1.8.4.2  nathanw #include <sys/time.h>
     48  1.8.4.2  nathanw #include <sys/syslog.h>
     49  1.8.4.2  nathanw 
     50  1.8.4.2  nathanw #include <machine/autoconf.h>
     51  1.8.4.2  nathanw #include <machine/openfirm.h>
     52  1.8.4.2  nathanw #include <machine/cpu.h>
     53  1.8.4.2  nathanw #include <machine/eeprom.h>
     54  1.8.4.2  nathanw #include <machine/psl.h>
     55  1.8.4.2  nathanw 
     56  1.8.4.2  nathanw #include <dev/cons.h>
     57  1.8.4.2  nathanw 
     58  1.8.4.2  nathanw #include <sparc64/dev/cons.h>
     59  1.8.4.2  nathanw 
     60  1.8.4.2  nathanw static int pconsmatch __P((struct device *, struct cfdata *, void *));
     61  1.8.4.2  nathanw static void pconsattach __P((struct device *, struct device *, void *));
     62  1.8.4.2  nathanw 
     63  1.8.4.2  nathanw struct cfattach pcons_ca = {
     64  1.8.4.2  nathanw 	sizeof(struct pconssoftc), pconsmatch, pconsattach
     65  1.8.4.2  nathanw };
     66  1.8.4.2  nathanw 
     67  1.8.4.2  nathanw extern struct cfdriver pcons_cd;
     68  1.8.4.3  nathanw 
     69  1.8.4.3  nathanw dev_type_open(pconsopen);
     70  1.8.4.3  nathanw dev_type_close(pconsclose);
     71  1.8.4.3  nathanw dev_type_read(pconsread);
     72  1.8.4.3  nathanw dev_type_write(pconswrite);
     73  1.8.4.3  nathanw dev_type_ioctl(pconsioctl);
     74  1.8.4.3  nathanw dev_type_tty(pconstty);
     75  1.8.4.3  nathanw dev_type_poll(pconspoll);
     76  1.8.4.3  nathanw 
     77  1.8.4.3  nathanw const struct cdevsw pcons_cdevsw = {
     78  1.8.4.3  nathanw 	pconsopen, pconsclose, pconsread, pconswrite, pconsioctl,
     79  1.8.4.3  nathanw 	nostop, pconstty, pconspoll, nommap, D_TTY
     80  1.8.4.3  nathanw };
     81  1.8.4.3  nathanw 
     82  1.8.4.2  nathanw static struct cnm_state pcons_cnm_state;
     83  1.8.4.2  nathanw 
     84  1.8.4.2  nathanw static int pconsprobe __P((void));
     85  1.8.4.2  nathanw extern struct consdev *cn_tab;
     86  1.8.4.2  nathanw 
     87  1.8.4.2  nathanw static int
     88  1.8.4.2  nathanw pconsmatch(parent, match, aux)
     89  1.8.4.2  nathanw 	struct device *parent;
     90  1.8.4.2  nathanw 	struct cfdata *match;
     91  1.8.4.2  nathanw 	void *aux;
     92  1.8.4.2  nathanw {
     93  1.8.4.2  nathanw 	struct mainbus_attach_args *ma = aux;
     94  1.8.4.2  nathanw 	extern int  prom_cngetc __P((dev_t));
     95  1.8.4.2  nathanw 
     96  1.8.4.2  nathanw 	/* Only attach if no other console has attached. */
     97  1.8.4.2  nathanw 	return ((strcmp("pcons", ma->ma_name) == 0) &&
     98  1.8.4.2  nathanw 		(cn_tab->cn_getc == prom_cngetc));
     99  1.8.4.2  nathanw 
    100  1.8.4.2  nathanw }
    101  1.8.4.2  nathanw 
    102  1.8.4.2  nathanw static void
    103  1.8.4.2  nathanw pconsattach(parent, self, aux)
    104  1.8.4.2  nathanw 	struct device *parent, *self;
    105  1.8.4.2  nathanw 	void *aux;
    106  1.8.4.2  nathanw {
    107  1.8.4.2  nathanw 	struct pconssoftc *sc = (struct pconssoftc *) self;
    108  1.8.4.2  nathanw 
    109  1.8.4.2  nathanw 	printf("\n");
    110  1.8.4.2  nathanw 	if (!pconsprobe())
    111  1.8.4.2  nathanw 		return;
    112  1.8.4.2  nathanw 
    113  1.8.4.2  nathanw 	cn_init_magic(&pcons_cnm_state);
    114  1.8.4.2  nathanw 	cn_set_magic("+++++");
    115  1.8.4.2  nathanw 	callout_init(&sc->sc_poll_ch);
    116  1.8.4.2  nathanw }
    117  1.8.4.2  nathanw 
    118  1.8.4.2  nathanw static void pconsstart __P((struct tty *));
    119  1.8.4.2  nathanw static int pconsparam __P((struct tty *, struct termios *));
    120  1.8.4.2  nathanw static void pcons_poll __P((void *));
    121  1.8.4.2  nathanw 
    122  1.8.4.2  nathanw int
    123  1.8.4.2  nathanw pconsopen(dev, flag, mode, p)
    124  1.8.4.2  nathanw 	dev_t dev;
    125  1.8.4.2  nathanw 	int flag, mode;
    126  1.8.4.2  nathanw 	struct proc *p;
    127  1.8.4.2  nathanw {
    128  1.8.4.2  nathanw 	struct pconssoftc *sc;
    129  1.8.4.2  nathanw 	int unit = minor(dev);
    130  1.8.4.2  nathanw 	struct tty *tp;
    131  1.8.4.2  nathanw 
    132  1.8.4.2  nathanw 	if (unit >= pcons_cd.cd_ndevs)
    133  1.8.4.2  nathanw 		return ENXIO;
    134  1.8.4.2  nathanw 	sc = pcons_cd.cd_devs[unit];
    135  1.8.4.2  nathanw 	if (!sc)
    136  1.8.4.2  nathanw 		return ENXIO;
    137  1.8.4.2  nathanw 	if (!(tp = sc->of_tty))
    138  1.8.4.2  nathanw 		sc->of_tty = tp = ttymalloc();
    139  1.8.4.2  nathanw 	tp->t_oproc = pconsstart;
    140  1.8.4.2  nathanw 	tp->t_param = pconsparam;
    141  1.8.4.2  nathanw 	tp->t_dev = dev;
    142  1.8.4.2  nathanw 	cn_tab->cn_dev = dev;
    143  1.8.4.2  nathanw 	if (!(tp->t_state & TS_ISOPEN)) {
    144  1.8.4.2  nathanw 		ttychars(tp);
    145  1.8.4.2  nathanw 		tp->t_iflag = TTYDEF_IFLAG;
    146  1.8.4.2  nathanw 		tp->t_oflag = TTYDEF_OFLAG;
    147  1.8.4.2  nathanw 		tp->t_cflag = TTYDEF_CFLAG;
    148  1.8.4.2  nathanw 		tp->t_lflag = TTYDEF_LFLAG;
    149  1.8.4.2  nathanw 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    150  1.8.4.2  nathanw 		pconsparam(tp, &tp->t_termios);
    151  1.8.4.2  nathanw 		ttsetwater(tp);
    152  1.8.4.2  nathanw 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    153  1.8.4.2  nathanw 		return EBUSY;
    154  1.8.4.2  nathanw 	tp->t_state |= TS_CARR_ON;
    155  1.8.4.2  nathanw 
    156  1.8.4.2  nathanw 	if (!(sc->of_flags & OFPOLL)) {
    157  1.8.4.2  nathanw 		sc->of_flags |= OFPOLL;
    158  1.8.4.2  nathanw 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    159  1.8.4.2  nathanw 	}
    160  1.8.4.2  nathanw 
    161  1.8.4.2  nathanw 	return (*tp->t_linesw->l_open)(dev, tp);
    162  1.8.4.2  nathanw }
    163  1.8.4.2  nathanw 
    164  1.8.4.2  nathanw int
    165  1.8.4.2  nathanw pconsclose(dev, flag, mode, p)
    166  1.8.4.2  nathanw 	dev_t dev;
    167  1.8.4.2  nathanw 	int flag, mode;
    168  1.8.4.2  nathanw 	struct proc *p;
    169  1.8.4.2  nathanw {
    170  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    171  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    172  1.8.4.2  nathanw 
    173  1.8.4.2  nathanw 	callout_stop(&sc->sc_poll_ch);
    174  1.8.4.2  nathanw 	sc->of_flags &= ~OFPOLL;
    175  1.8.4.2  nathanw 	(*tp->t_linesw->l_close)(tp, flag);
    176  1.8.4.2  nathanw 	ttyclose(tp);
    177  1.8.4.2  nathanw 	return 0;
    178  1.8.4.2  nathanw }
    179  1.8.4.2  nathanw 
    180  1.8.4.2  nathanw int
    181  1.8.4.2  nathanw pconsread(dev, uio, flag)
    182  1.8.4.2  nathanw 	dev_t dev;
    183  1.8.4.2  nathanw 	struct uio *uio;
    184  1.8.4.2  nathanw 	int flag;
    185  1.8.4.2  nathanw {
    186  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    187  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    188  1.8.4.2  nathanw 
    189  1.8.4.2  nathanw 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    190  1.8.4.2  nathanw }
    191  1.8.4.2  nathanw 
    192  1.8.4.2  nathanw int
    193  1.8.4.2  nathanw pconswrite(dev, uio, flag)
    194  1.8.4.2  nathanw 	dev_t dev;
    195  1.8.4.2  nathanw 	struct uio *uio;
    196  1.8.4.2  nathanw 	int flag;
    197  1.8.4.2  nathanw {
    198  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    199  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    200  1.8.4.2  nathanw 
    201  1.8.4.2  nathanw 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    202  1.8.4.2  nathanw }
    203  1.8.4.2  nathanw 
    204  1.8.4.2  nathanw int
    205  1.8.4.2  nathanw pconspoll(dev, events, p)
    206  1.8.4.2  nathanw 	dev_t dev;
    207  1.8.4.2  nathanw 	int events;
    208  1.8.4.2  nathanw 	struct proc *p;
    209  1.8.4.2  nathanw {
    210  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    211  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    212  1.8.4.2  nathanw 
    213  1.8.4.2  nathanw 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    214  1.8.4.2  nathanw }
    215  1.8.4.2  nathanw 
    216  1.8.4.2  nathanw int
    217  1.8.4.2  nathanw pconsioctl(dev, cmd, data, flag, p)
    218  1.8.4.2  nathanw 	dev_t dev;
    219  1.8.4.2  nathanw 	u_long cmd;
    220  1.8.4.2  nathanw 	caddr_t data;
    221  1.8.4.2  nathanw 	int flag;
    222  1.8.4.2  nathanw 	struct proc *p;
    223  1.8.4.2  nathanw {
    224  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    225  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    226  1.8.4.2  nathanw 	int error;
    227  1.8.4.2  nathanw 
    228  1.8.4.2  nathanw 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) != EPASSTHROUGH)
    229  1.8.4.2  nathanw 		return error;
    230  1.8.4.2  nathanw 	return ttioctl(tp, cmd, data, flag, p);
    231  1.8.4.2  nathanw }
    232  1.8.4.2  nathanw 
    233  1.8.4.2  nathanw struct tty *
    234  1.8.4.2  nathanw pconstty(dev)
    235  1.8.4.2  nathanw 	dev_t dev;
    236  1.8.4.2  nathanw {
    237  1.8.4.2  nathanw 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    238  1.8.4.2  nathanw 
    239  1.8.4.2  nathanw 	return sc->of_tty;
    240  1.8.4.2  nathanw }
    241  1.8.4.2  nathanw 
    242  1.8.4.2  nathanw static void
    243  1.8.4.2  nathanw pconsstart(tp)
    244  1.8.4.2  nathanw 	struct tty *tp;
    245  1.8.4.2  nathanw {
    246  1.8.4.2  nathanw 	struct clist *cl;
    247  1.8.4.2  nathanw 	int s, len;
    248  1.8.4.2  nathanw 	u_char buf[OFBURSTLEN];
    249  1.8.4.2  nathanw 
    250  1.8.4.2  nathanw 	s = spltty();
    251  1.8.4.2  nathanw 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    252  1.8.4.2  nathanw 		splx(s);
    253  1.8.4.2  nathanw 		return;
    254  1.8.4.2  nathanw 	}
    255  1.8.4.2  nathanw 	tp->t_state |= TS_BUSY;
    256  1.8.4.2  nathanw 	splx(s);
    257  1.8.4.2  nathanw 	cl = &tp->t_outq;
    258  1.8.4.2  nathanw 	len = q_to_b(cl, buf, OFBURSTLEN);
    259  1.8.4.2  nathanw 	OF_write(stdout, buf, len);
    260  1.8.4.2  nathanw 	s = spltty();
    261  1.8.4.2  nathanw 	tp->t_state &= ~TS_BUSY;
    262  1.8.4.2  nathanw 	if (cl->c_cc) {
    263  1.8.4.2  nathanw 		tp->t_state |= TS_TIMEOUT;
    264  1.8.4.2  nathanw 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    265  1.8.4.2  nathanw 	}
    266  1.8.4.2  nathanw 	if (cl->c_cc <= tp->t_lowat) {
    267  1.8.4.2  nathanw 		if (tp->t_state & TS_ASLEEP) {
    268  1.8.4.2  nathanw 			tp->t_state &= ~TS_ASLEEP;
    269  1.8.4.2  nathanw 			wakeup(cl);
    270  1.8.4.2  nathanw 		}
    271  1.8.4.2  nathanw 		selwakeup(&tp->t_wsel);
    272  1.8.4.2  nathanw 	}
    273  1.8.4.2  nathanw 	splx(s);
    274  1.8.4.2  nathanw }
    275  1.8.4.2  nathanw 
    276  1.8.4.2  nathanw static int
    277  1.8.4.2  nathanw pconsparam(tp, t)
    278  1.8.4.2  nathanw 	struct tty *tp;
    279  1.8.4.2  nathanw 	struct termios *t;
    280  1.8.4.2  nathanw {
    281  1.8.4.2  nathanw 	tp->t_ispeed = t->c_ispeed;
    282  1.8.4.2  nathanw 	tp->t_ospeed = t->c_ospeed;
    283  1.8.4.2  nathanw 	tp->t_cflag = t->c_cflag;
    284  1.8.4.2  nathanw 	return 0;
    285  1.8.4.2  nathanw }
    286  1.8.4.2  nathanw 
    287  1.8.4.2  nathanw static void
    288  1.8.4.2  nathanw pcons_poll(aux)
    289  1.8.4.2  nathanw 	void *aux;
    290  1.8.4.2  nathanw {
    291  1.8.4.2  nathanw 	struct pconssoftc *sc = aux;
    292  1.8.4.2  nathanw 	struct tty *tp = sc->of_tty;
    293  1.8.4.2  nathanw 	char ch;
    294  1.8.4.2  nathanw 
    295  1.8.4.2  nathanw 	while (OF_read(stdin, &ch, 1) > 0) {
    296  1.8.4.2  nathanw 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
    297  1.8.4.2  nathanw 		if (tp && (tp->t_state & TS_ISOPEN))
    298  1.8.4.2  nathanw 			(*tp->t_linesw->l_rint)(ch, tp);
    299  1.8.4.2  nathanw 	}
    300  1.8.4.2  nathanw 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    301  1.8.4.2  nathanw }
    302  1.8.4.2  nathanw 
    303  1.8.4.2  nathanw int
    304  1.8.4.2  nathanw pconsprobe()
    305  1.8.4.2  nathanw {
    306  1.8.4.2  nathanw 	if (!stdin) stdin = OF_stdin();
    307  1.8.4.2  nathanw 	if (!stdout) stdout = OF_stdout();
    308  1.8.4.2  nathanw 
    309  1.8.4.2  nathanw 	return (stdin && stdout);
    310  1.8.4.2  nathanw }
    311  1.8.4.2  nathanw 
    312  1.8.4.2  nathanw void
    313  1.8.4.2  nathanw pcons_cnpollc(dev, on)
    314  1.8.4.2  nathanw 	dev_t dev;
    315  1.8.4.2  nathanw 	int on;
    316  1.8.4.2  nathanw {
    317  1.8.4.2  nathanw 	struct pconssoftc *sc = NULL;
    318  1.8.4.2  nathanw 
    319  1.8.4.2  nathanw 	if (pcons_cd.cd_devs)
    320  1.8.4.2  nathanw 		sc = pcons_cd.cd_devs[minor(dev)];
    321  1.8.4.2  nathanw 
    322  1.8.4.2  nathanw 	if (on) {
    323  1.8.4.2  nathanw 		if (!sc) return;
    324  1.8.4.2  nathanw 		if (sc->of_flags & OFPOLL)
    325  1.8.4.2  nathanw 			callout_stop(&sc->sc_poll_ch);
    326  1.8.4.2  nathanw 		sc->of_flags &= ~OFPOLL;
    327  1.8.4.2  nathanw 	} else {
    328  1.8.4.2  nathanw                 /* Resuming kernel. */
    329  1.8.4.2  nathanw 		if (sc && !(sc->of_flags & OFPOLL)) {
    330  1.8.4.2  nathanw 			sc->of_flags |= OFPOLL;
    331  1.8.4.2  nathanw 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    332  1.8.4.2  nathanw 		}
    333  1.8.4.2  nathanw 	}
    334  1.8.4.2  nathanw }
    335  1.8.4.2  nathanw 
    336  1.8.4.2  nathanw void pcons_dopoll __P((void));
    337  1.8.4.2  nathanw void
    338  1.8.4.2  nathanw pcons_dopoll() {
    339  1.8.4.2  nathanw 		pcons_poll((void*)pcons_cd.cd_devs[0]);
    340  1.8.4.2  nathanw }
    341