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