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