Home | History | Annotate | Line # | Download | only in ofw
ofcons.c revision 1.22
      1 /*	$NetBSD: ofcons.c,v 1.22 2005/02/04 02:10:44 perry 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: ofcons.c,v 1.22 2005/02/04 02:10:44 perry Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/conf.h>
     39 #include <sys/device.h>
     40 #include <sys/proc.h>
     41 #include <sys/systm.h>
     42 #include <sys/callout.h>
     43 #include <sys/tty.h>
     44 
     45 #include <dev/cons.h>
     46 
     47 #include <dev/ofw/openfirm.h>
     48 
     49 struct ofcons_softc {
     50 	struct device of_dev;
     51 	struct tty *of_tty;
     52 	struct callout sc_poll_ch;
     53 	int of_flags;
     54 };
     55 /* flags: */
     56 #define	OFPOLL		1
     57 
     58 #define	OFBURSTLEN	128	/* max number of bytes to write in one chunk */
     59 
     60 cons_decl(ofcons_);
     61 
     62 static int stdin, stdout;
     63 
     64 static int ofcons_match(struct device *, struct cfdata *, void *);
     65 static void ofcons_attach(struct device *, struct device *, void *);
     66 
     67 CFATTACH_DECL(ofcons, sizeof(struct ofcons_softc),
     68     ofcons_match, ofcons_attach, NULL, NULL);
     69 
     70 extern struct cfdriver ofcons_cd;
     71 
     72 dev_type_open(ofcons_open);
     73 dev_type_close(ofcons_close);
     74 dev_type_read(ofcons_read);
     75 dev_type_write(ofcons_write);
     76 dev_type_ioctl(ofcons_ioctl);
     77 dev_type_tty(ofcons_tty);
     78 dev_type_poll(ofcons_poll);
     79 
     80 const struct cdevsw ofcons_cdevsw = {
     81 	ofcons_open, ofcons_close, ofcons_read, ofcons_write, ofcons_ioctl,
     82 	nostop, ofcons_tty, ofcons_poll, nommap, ttykqfilter, D_TTY
     83 };
     84 
     85 static int ofcons_probe(void);
     86 
     87 static int
     88 ofcons_match(parent, match, aux)
     89 	struct device *parent;
     90 	struct cfdata *match;
     91 	void *aux;
     92 {
     93 	struct ofbus_attach_args *oba = aux;
     94 
     95 	if (strcmp(oba->oba_busname, "ofw"))
     96 		return (0);
     97 	if (!ofcons_probe())
     98 		return 0;
     99 	return OF_instance_to_package(stdin) == oba->oba_phandle
    100 		|| OF_instance_to_package(stdout) == oba->oba_phandle;
    101 }
    102 
    103 static void
    104 ofcons_attach(parent, self, aux)
    105 	struct device *parent, *self;
    106 	void *aux;
    107 {
    108 	struct ofcons_softc *sc = (struct ofcons_softc *) self;
    109 
    110 	printf("\n");
    111 
    112 	callout_init(&sc->sc_poll_ch);
    113 }
    114 
    115 static void ofcons_start(struct tty *);
    116 static int ofcons_param(struct tty *, struct termios *);
    117 static void ofcons_pollin(void *);
    118 
    119 int
    120 ofcons_open(dev, flag, mode, p)
    121 	dev_t dev;
    122 	int flag, mode;
    123 	struct proc *p;
    124 {
    125 	struct ofcons_softc *sc;
    126 	int unit = minor(dev);
    127 	struct tty *tp;
    128 
    129 	if (unit >= ofcons_cd.cd_ndevs)
    130 		return ENXIO;
    131 	sc = ofcons_cd.cd_devs[unit];
    132 	if (!sc)
    133 		return ENXIO;
    134 	if (!(tp = sc->of_tty))
    135 		sc->of_tty = tp = ttymalloc();
    136 	tp->t_oproc = ofcons_start;
    137 	tp->t_param = ofcons_param;
    138 	tp->t_dev = dev;
    139 	if (!(tp->t_state & TS_ISOPEN)) {
    140 		ttychars(tp);
    141 		tp->t_iflag = TTYDEF_IFLAG;
    142 		tp->t_oflag = TTYDEF_OFLAG;
    143 		tp->t_cflag = TTYDEF_CFLAG;
    144 		tp->t_lflag = TTYDEF_LFLAG;
    145 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    146 		ofcons_param(tp, &tp->t_termios);
    147 		ttsetwater(tp);
    148 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    149 		return EBUSY;
    150 	tp->t_state |= TS_CARR_ON;
    151 
    152 	if (!(sc->of_flags & OFPOLL)) {
    153 		sc->of_flags |= OFPOLL;
    154 		callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
    155 	}
    156 
    157 	return (*tp->t_linesw->l_open)(dev, tp);
    158 }
    159 
    160 int
    161 ofcons_close(dev, flag, mode, p)
    162 	dev_t dev;
    163 	int flag, mode;
    164 	struct proc *p;
    165 {
    166 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    167 	struct tty *tp = sc->of_tty;
    168 
    169 	callout_stop(&sc->sc_poll_ch);
    170 	sc->of_flags &= ~OFPOLL;
    171 	(*tp->t_linesw->l_close)(tp, flag);
    172 	ttyclose(tp);
    173 	return 0;
    174 }
    175 
    176 int
    177 ofcons_read(dev, uio, flag)
    178 	dev_t dev;
    179 	struct uio *uio;
    180 	int flag;
    181 {
    182 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    183 	struct tty *tp = sc->of_tty;
    184 
    185 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    186 }
    187 
    188 int
    189 ofcons_write(dev, uio, flag)
    190 	dev_t dev;
    191 	struct uio *uio;
    192 	int flag;
    193 {
    194 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    195 	struct tty *tp = sc->of_tty;
    196 
    197 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    198 }
    199 
    200 int
    201 ofcons_poll(dev, events, p)
    202 	dev_t dev;
    203 	int events;
    204 	struct proc *p;
    205 {
    206 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    207 	struct tty *tp = sc->of_tty;
    208 
    209 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    210 }
    211 int
    212 ofcons_ioctl(dev, cmd, data, flag, p)
    213 	dev_t dev;
    214 	u_long cmd;
    215 	caddr_t data;
    216 	int flag;
    217 	struct proc *p;
    218 {
    219 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    220 	struct tty *tp = sc->of_tty;
    221 	int error;
    222 
    223 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) != EPASSTHROUGH)
    224 		return error;
    225 	return ttioctl(tp, cmd, data, flag, p);
    226 }
    227 
    228 struct tty *
    229 ofcons_tty(dev)
    230 	dev_t dev;
    231 {
    232 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    233 
    234 	return sc->of_tty;
    235 }
    236 
    237 static void
    238 ofcons_start(tp)
    239 	struct tty *tp;
    240 {
    241 	struct clist *cl;
    242 	int s, len;
    243 	u_char buf[OFBURSTLEN];
    244 
    245 	s = spltty();
    246 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    247 		splx(s);
    248 		return;
    249 	}
    250 	tp->t_state |= TS_BUSY;
    251 	splx(s);
    252 	cl = &tp->t_outq;
    253 	len = q_to_b(cl, buf, OFBURSTLEN);
    254 	OF_write(stdout, buf, len);
    255 	s = spltty();
    256 	tp->t_state &= ~TS_BUSY;
    257 	if (cl->c_cc) {
    258 		tp->t_state |= TS_TIMEOUT;
    259 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    260 	}
    261 	if (cl->c_cc <= tp->t_lowat) {
    262 		if (tp->t_state & TS_ASLEEP) {
    263 			tp->t_state &= ~TS_ASLEEP;
    264 			wakeup(cl);
    265 		}
    266 		selwakeup(&tp->t_wsel);
    267 	}
    268 	splx(s);
    269 }
    270 
    271 static int
    272 ofcons_param(tp, t)
    273 	struct tty *tp;
    274 	struct termios *t;
    275 {
    276 	tp->t_ispeed = t->c_ispeed;
    277 	tp->t_ospeed = t->c_ospeed;
    278 	tp->t_cflag = t->c_cflag;
    279 	return 0;
    280 }
    281 
    282 static void
    283 ofcons_pollin(aux)
    284 	void *aux;
    285 {
    286 	struct ofcons_softc *sc = aux;
    287 	struct tty *tp = sc->of_tty;
    288 	char ch;
    289 
    290 	while (OF_read(stdin, &ch, 1) > 0) {
    291 		if (tp && (tp->t_state & TS_ISOPEN))
    292 			(*tp->t_linesw->l_rint)(ch, tp);
    293 	}
    294 	callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
    295 }
    296 
    297 static int
    298 ofcons_probe()
    299 {
    300 	int chosen;
    301 	char stdinbuf[4], stdoutbuf[4];
    302 
    303 	if (stdin)
    304 		return 1;
    305 	if ((chosen = OF_finddevice("/chosen")) == -1)
    306 		return 0;
    307 	if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) !=
    308 	      sizeof stdinbuf ||
    309 	    OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) !=
    310 	      sizeof stdoutbuf)
    311 		return 0;
    312 
    313 	/* Decode properties. */
    314 	stdin = of_decode_int(stdinbuf);
    315 	stdout = of_decode_int(stdoutbuf);
    316 
    317 	return 1;
    318 }
    319 
    320 void
    321 ofcons_cnprobe(cd)
    322 	struct consdev *cd;
    323 {
    324 	int maj;
    325 
    326 	if (!ofcons_probe())
    327 		return;
    328 
    329 	maj = cdevsw_lookup_major(&ofcons_cdevsw);
    330 	cd->cn_dev = makedev(maj, 0);
    331 	cd->cn_pri = CN_INTERNAL;
    332 }
    333 
    334 void
    335 ofcons_cninit(cd)
    336 	struct consdev *cd;
    337 {
    338 }
    339 
    340 int
    341 ofcons_cngetc(dev)
    342 	dev_t dev;
    343 {
    344 	unsigned char ch = '\0';
    345 	int l;
    346 
    347 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    348 		if (l != -2 && l != 0)
    349 			return -1;
    350 	return ch;
    351 }
    352 
    353 void
    354 ofcons_cnputc(dev, c)
    355 	dev_t dev;
    356 	int c;
    357 {
    358 	char ch = c;
    359 
    360 	OF_write(stdout, &ch, 1);
    361 }
    362 
    363 void
    364 ofcons_cnpollc(dev, on)
    365 	dev_t dev;
    366 	int on;
    367 {
    368 	struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)];
    369 
    370 	if (!sc)
    371 		return;
    372 	if (on) {
    373 		if (sc->of_flags & OFPOLL)
    374 			callout_stop(&sc->sc_poll_ch);
    375 		sc->of_flags &= ~OFPOLL;
    376 	} else {
    377 		if (!(sc->of_flags & OFPOLL)) {
    378 			sc->of_flags |= OFPOLL;
    379 			callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
    380 		}
    381 	}
    382 }
    383