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