Home | History | Annotate | Line # | Download | only in ofw
ofcons.c revision 1.10
      1 /*	$NetBSD: ofcons.c,v 1.10 2000/03/23 07:01:37 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/conf.h>
     36 #include <sys/device.h>
     37 #include <sys/proc.h>
     38 #include <sys/systm.h>
     39 #include <sys/callout.h>
     40 #include <sys/tty.h>
     41 
     42 #include <dev/cons.h>
     43 
     44 #include <dev/ofw/openfirm.h>
     45 
     46 struct ofcons_softc {
     47 	struct device of_dev;
     48 	struct tty *of_tty;
     49 	struct callout sc_poll_ch;
     50 	int of_flags;
     51 };
     52 /* flags: */
     53 #define	OFPOLL		1
     54 
     55 #define	OFBURSTLEN	128	/* max number of bytes to write in one chunk */
     56 
     57 static int stdin, stdout;
     58 
     59 static int ofcons_match __P((struct device *, struct cfdata *, void *));
     60 static void ofcons_attach __P((struct device *, struct device *, void *));
     61 
     62 struct cfattach ofcons_ca = {
     63 	sizeof(struct ofcons_softc), ofcons_match, ofcons_attach
     64 };
     65 
     66 extern struct cfdriver ofcons_cd;
     67 
     68 static int ofcons_probe __P((void));
     69 
     70 static int
     71 ofcons_match(parent, match, aux)
     72 	struct device *parent;
     73 	struct cfdata *match;
     74 	void *aux;
     75 {
     76 	struct ofbus_attach_args *oba = aux;
     77 
     78 	if (strcmp(oba->oba_busname, "ofw"))
     79 		return (0);
     80 	if (!ofcons_probe())
     81 		return 0;
     82 	return OF_instance_to_package(stdin) == oba->oba_phandle
     83 		|| OF_instance_to_package(stdout) == oba->oba_phandle;
     84 }
     85 
     86 static void
     87 ofcons_attach(parent, self, aux)
     88 	struct device *parent, *self;
     89 	void *aux;
     90 {
     91 	printf("\n");
     92 
     93 	callout_reset(&sc->sc_poll_ch);
     94 }
     95 
     96 static void ofcons_start __P((struct tty *));
     97 static int ofcons_param __P((struct tty *, struct termios *));
     98 static void ofcons_poll __P((void *));
     99 
    100 int
    101 ofcons_open(dev, flag, mode, p)
    102 	dev_t dev;
    103 	int flag, mode;
    104 	struct proc *p;
    105 {
    106 	struct ofcons_softc *sc;
    107 	int unit = minor(dev);
    108 	struct tty *tp;
    109 
    110 	if (unit >= ofcons_cd.cd_ndevs)
    111 		return ENXIO;
    112 	sc = ofcons_cd.cd_devs[unit];
    113 	if (!sc)
    114 		return ENXIO;
    115 	if (!(tp = sc->of_tty))
    116 		sc->of_tty = tp = ttymalloc();
    117 	tp->t_oproc = ofcons_start;
    118 	tp->t_param = ofcons_param;
    119 	tp->t_dev = dev;
    120 	if (!(tp->t_state & TS_ISOPEN)) {
    121 		ttychars(tp);
    122 		tp->t_iflag = TTYDEF_IFLAG;
    123 		tp->t_oflag = TTYDEF_OFLAG;
    124 		tp->t_cflag = TTYDEF_CFLAG;
    125 		tp->t_lflag = TTYDEF_LFLAG;
    126 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    127 		ofcons_param(tp, &tp->t_termios);
    128 		ttsetwater(tp);
    129 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    130 		return EBUSY;
    131 	tp->t_state |= TS_CARR_ON;
    132 
    133 	if (!(sc->of_flags & OFPOLL)) {
    134 		sc->of_flags |= OFPOLL;
    135 		callout_reset(&sc->sc_poll_ch, 1, ofcons_poll, 1);
    136 	}
    137 
    138 	return (*linesw[tp->t_line].l_open)(dev, tp);
    139 }
    140 
    141 int
    142 ofcons_close(dev, flag, mode, p)
    143 	dev_t dev;
    144 	int flag, mode;
    145 	struct proc *p;
    146 {
    147 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    148 	struct tty *tp = sc->of_tty;
    149 
    150 	callout_stop(&sc->sc_poll_ch);
    151 	sc->of_flags &= ~OFPOLL;
    152 	(*linesw[tp->t_line].l_close)(tp, flag);
    153 	ttyclose(tp);
    154 	return 0;
    155 }
    156 
    157 int
    158 ofcons_read(dev, uio, flag)
    159 	dev_t dev;
    160 	struct uio *uio;
    161 	int flag;
    162 {
    163 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    164 	struct tty *tp = sc->of_tty;
    165 
    166 	return (*linesw[tp->t_line].l_read)(tp, uio, flag);
    167 }
    168 
    169 int
    170 ofcons_write(dev, uio, flag)
    171 	dev_t dev;
    172 	struct uio *uio;
    173 	int flag;
    174 {
    175 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    176 	struct tty *tp = sc->of_tty;
    177 
    178 	return (*linesw[tp->t_line].l_write)(tp, uio, flag);
    179 }
    180 
    181 int
    182 ofcons_ioctl(dev, cmd, data, flag, p)
    183 	dev_t dev;
    184 	u_long cmd;
    185 	caddr_t data;
    186 	int flag;
    187 	struct proc *p;
    188 {
    189 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    190 	struct tty *tp = sc->of_tty;
    191 	int error;
    192 
    193 	if ((error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p)) >= 0)
    194 		return error;
    195 	if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
    196 		return error;
    197 	return ENOTTY;
    198 }
    199 
    200 struct tty *
    201 ofcons_tty(dev)
    202 	dev_t dev;
    203 {
    204 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    205 
    206 	return sc->of_tty;
    207 }
    208 
    209 void
    210 ofcons_stop(tp, flag)
    211 	struct tty *tp;
    212 	int flag;
    213 {
    214 }
    215 
    216 static void
    217 ofcons_start(tp)
    218 	struct tty *tp;
    219 {
    220 	struct clist *cl;
    221 	int s, len;
    222 	u_char buf[OFBURSTLEN];
    223 
    224 	s = spltty();
    225 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    226 		splx(s);
    227 		return;
    228 	}
    229 	tp->t_state |= TS_BUSY;
    230 	splx(s);
    231 	cl = &tp->t_outq;
    232 	len = q_to_b(cl, buf, OFBURSTLEN);
    233 	OF_write(stdout, buf, len);
    234 	s = spltty();
    235 	tp->t_state &= ~TS_BUSY;
    236 	if (cl->c_cc) {
    237 		tp->t_state |= TS_TIMEOUT;
    238 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    239 	}
    240 	if (cl->c_cc <= tp->t_lowat) {
    241 		if (tp->t_state & TS_ASLEEP) {
    242 			tp->t_state &= ~TS_ASLEEP;
    243 			wakeup(cl);
    244 		}
    245 		selwakeup(&tp->t_wsel);
    246 	}
    247 	splx(s);
    248 }
    249 
    250 static int
    251 ofcons_param(tp, t)
    252 	struct tty *tp;
    253 	struct termios *t;
    254 {
    255 	tp->t_ispeed = t->c_ispeed;
    256 	tp->t_ospeed = t->c_ospeed;
    257 	tp->t_cflag = t->c_cflag;
    258 	return 0;
    259 }
    260 
    261 static void
    262 ofcons_poll(aux)
    263 	void *aux;
    264 {
    265 	struct ofcons_softc *sc = aux;
    266 	struct tty *tp = sc->of_tty;
    267 	char ch;
    268 
    269 	while (OF_read(stdin, &ch, 1) > 0) {
    270 		if (tp && (tp->t_state & TS_ISOPEN))
    271 			(*linesw[tp->t_line].l_rint)(ch, tp);
    272 	}
    273 	callout_reset(&sc->sc_poll_ch, 1, ofcons_poll, 1);
    274 }
    275 
    276 static int
    277 ofcons_probe()
    278 {
    279 	int chosen;
    280 	char stdinbuf[4], stdoutbuf[4];
    281 
    282 	if (stdin)
    283 		return 1;
    284 	if ((chosen = OF_finddevice("/chosen")) == -1)
    285 		return 0;
    286 	if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
    287 	      sizeof stdinbuf ||
    288 	    OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
    289 	      sizeof stdoutbuf)
    290 		return 0;
    291 
    292 	/* Decode properties. */
    293 	stdin = of_decode_int(stdinbuf);
    294 	stdout = of_decode_int(stdoutbuf);
    295 
    296 	return 1;
    297 }
    298 
    299 void
    300 ofcons_cnprobe(cd)
    301 	struct consdev *cd;
    302 {
    303 	int maj;
    304 
    305 	if (!ofcons_probe())
    306 		return;
    307 
    308 	for (maj = 0; maj < nchrdev; maj++)
    309 		if (cdevsw[maj].d_open == ofcons_open)
    310 			break;
    311 	cd->cn_dev = makedev(maj, 0);
    312 	cd->cn_pri = CN_INTERNAL;
    313 }
    314 
    315 void
    316 ofcons_cninit(cd)
    317 	struct consdev *cd;
    318 {
    319 }
    320 
    321 int
    322 ofcons_cngetc(dev)
    323 	dev_t dev;
    324 {
    325 	unsigned char ch = '\0';
    326 	int l;
    327 
    328 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    329 		if (l != -2 && l != 0)
    330 			return -1;
    331 	return ch;
    332 }
    333 
    334 void
    335 ofcons_cnputc(dev, c)
    336 	dev_t dev;
    337 	int c;
    338 {
    339 	char ch = c;
    340 
    341 	OF_write(stdout, &ch, 1);
    342 }
    343 
    344 void
    345 ofcons_cnpollc(dev, on)
    346 	dev_t dev;
    347 	int on;
    348 {
    349 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    350 
    351 	if (!sc)
    352 		return;
    353 	if (on) {
    354 		if (sc->of_flags & OFPOLL)
    355 			callout_stop(&sc->sc_poll_ch);
    356 		sc->of_flags &= ~OFPOLL;
    357 	} else {
    358 		if (!(sc->of_flags & OFPOLL)) {
    359 			sc->of_flags |= OFPOLL;
    360 			callout_reset(&sc->sc_poll_ch, 1, ofcons_poll, sc);
    361 		}
    362 	}
    363 }
    364