Home | History | Annotate | Line # | Download | only in ofw
      1 /*	$NetBSD: ofcons.c,v 1.45 2014/07/25 08:10:37 dholland 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.45 2014/07/25 08:10:37 dholland 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 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(device_t, cfdata_t, void *);
     65 static void ofcons_attach(device_t, device_t, void *);
     66 
     67 CFATTACH_DECL_NEW(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 	.d_open = ofcons_open,
     82 	.d_close = ofcons_close,
     83 	.d_read = ofcons_read,
     84 	.d_write = ofcons_write,
     85 	.d_ioctl = ofcons_ioctl,
     86 	.d_stop = nostop,
     87 	.d_tty = ofcons_tty,
     88 	.d_poll = ofcons_poll,
     89 	.d_mmap = nommap,
     90 	.d_kqfilter = ttykqfilter,
     91 	.d_discard = nodiscard,
     92 	.d_flag = D_TTY
     93 };
     94 
     95 static int ofcons_probe(void);
     96 
     97 static int
     98 ofcons_match(device_t parent, cfdata_t match, void *aux)
     99 {
    100 	struct ofbus_attach_args *oba = aux;
    101 
    102 	if (strcmp(oba->oba_busname, "ofw"))
    103 		return (0);
    104 	if (!ofcons_probe())
    105 		return 0;
    106 	return OF_instance_to_package(stdin) == oba->oba_phandle
    107 		|| OF_instance_to_package(stdout) == oba->oba_phandle;
    108 }
    109 
    110 static void
    111 ofcons_attach(device_t parent, device_t self, void *aux)
    112 {
    113 	struct ofcons_softc *sc = device_private(self);
    114 
    115 	printf("\n");
    116 
    117 	callout_init(&sc->sc_poll_ch, 0);
    118 }
    119 
    120 static void ofcons_start(struct tty *);
    121 static int ofcons_param(struct tty *, struct termios *);
    122 static void ofcons_pollin(void *);
    123 
    124 int
    125 ofcons_open(dev_t dev, int flag, int mode, struct lwp *l)
    126 {
    127 	struct ofcons_softc *sc;
    128 	struct tty *tp;
    129 
    130 	sc = device_lookup_private(&ofcons_cd, minor(dev));
    131 	if (!sc)
    132 		return ENXIO;
    133 	if (!(tp = sc->of_tty))
    134 		sc->of_tty = tp = tty_alloc();
    135 	tp->t_oproc = ofcons_start;
    136 	tp->t_param = ofcons_param;
    137 	tp->t_dev = dev;
    138 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    139 		return (EBUSY);
    140 	if (!(tp->t_state & TS_ISOPEN)) {
    141 		ttychars(tp);
    142 		tp->t_iflag = TTYDEF_IFLAG;
    143 		tp->t_oflag = TTYDEF_OFLAG;
    144 		tp->t_cflag = TTYDEF_CFLAG;
    145 		tp->t_lflag = TTYDEF_LFLAG;
    146 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    147 		ofcons_param(tp, &tp->t_termios);
    148 		ttsetwater(tp);
    149 	}
    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_t dev, int flag, int mode, struct lwp *l)
    162 {
    163 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    164 	struct tty *tp = sc->of_tty;
    165 
    166 	callout_stop(&sc->sc_poll_ch);
    167 	sc->of_flags &= ~OFPOLL;
    168 	(*tp->t_linesw->l_close)(tp, flag);
    169 	ttyclose(tp);
    170 	return 0;
    171 }
    172 
    173 int
    174 ofcons_read(dev_t dev, struct uio *uio, int flag)
    175 {
    176 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    177 	struct tty *tp = sc->of_tty;
    178 
    179 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    180 }
    181 
    182 int
    183 ofcons_write(dev_t dev, struct uio *uio, int flag)
    184 {
    185 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    186 	struct tty *tp = sc->of_tty;
    187 
    188 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    189 }
    190 
    191 int
    192 ofcons_poll(dev_t dev, int events, struct lwp *l)
    193 {
    194 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    195 	struct tty *tp = sc->of_tty;
    196 
    197 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    198 }
    199 int
    200 ofcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    201 {
    202 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    203 	struct tty *tp = sc->of_tty;
    204 	int error;
    205 
    206 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
    207 		return error;
    208 	return ttioctl(tp, cmd, data, flag, l);
    209 }
    210 
    211 struct tty *
    212 ofcons_tty(dev_t dev)
    213 {
    214 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    215 
    216 	return sc->of_tty;
    217 }
    218 
    219 static void
    220 ofcons_start(struct tty *tp)
    221 {
    222 	int s, len;
    223 	u_char buf[OFBURSTLEN];
    224 
    225 	s = spltty();
    226 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    227 		splx(s);
    228 		return;
    229 	}
    230 	tp->t_state |= TS_BUSY;
    231 	splx(s);
    232 	len = q_to_b(&tp->t_outq, buf, OFBURSTLEN);
    233 	OF_write(stdout, buf, len);
    234 	s = spltty();
    235 	tp->t_state &= ~TS_BUSY;
    236 	if (ttypull(tp)) {
    237 		tp->t_state |= TS_TIMEOUT;
    238 		callout_schedule(&tp->t_rstrt_ch, 1);
    239 	}
    240 	splx(s);
    241 }
    242 
    243 static int
    244 ofcons_param(struct tty *tp, struct termios *t)
    245 {
    246 	tp->t_ispeed = t->c_ispeed;
    247 	tp->t_ospeed = t->c_ospeed;
    248 	tp->t_cflag = t->c_cflag;
    249 	return 0;
    250 }
    251 
    252 static void
    253 ofcons_pollin(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(void)
    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(struct consdev *cd)
    291 {
    292 	int maj;
    293 
    294 	if (!ofcons_probe())
    295 		return;
    296 
    297 	maj = cdevsw_lookup_major(&ofcons_cdevsw);
    298 	cd->cn_dev = makedev(maj, 0);
    299 	cd->cn_pri = CN_INTERNAL;
    300 }
    301 
    302 void
    303 ofcons_cninit(struct consdev *cd)
    304 {
    305 }
    306 
    307 int
    308 ofcons_cngetc(dev_t dev)
    309 {
    310 	unsigned char ch = '\0';
    311 	int l;
    312 
    313 	while ((l = OF_read(stdin, &ch, 1)) != 1)
    314 		if (l != -2 && l != 0)
    315 			return -1;
    316 	return ch;
    317 }
    318 
    319 void
    320 ofcons_cnputc(dev_t dev, int c)
    321 {
    322 	char ch = c;
    323 
    324 	OF_write(stdout, &ch, 1);
    325 }
    326 
    327 void
    328 ofcons_cnpollc(dev_t dev, int on)
    329 {
    330 	struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev));
    331 
    332 	if (!sc)
    333 		return;
    334 	if (on) {
    335 		if (sc->of_flags & OFPOLL)
    336 			callout_stop(&sc->sc_poll_ch);
    337 		sc->of_flags &= ~OFPOLL;
    338 	} else {
    339 		if (!(sc->of_flags & OFPOLL)) {
    340 			sc->of_flags |= OFPOLL;
    341 			callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc);
    342 		}
    343 	}
    344 }
    345