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