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