Home | History | Annotate | Line # | Download | only in news68k
romcons.c revision 1.1
      1  1.1  tsutsui /*	$NetBSD: romcons.c,v 1.1 2011/11/20 15:38:00 tsutsui Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  1.1  tsutsui  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  1.1  tsutsui  * All rights reserved.
      7  1.1  tsutsui  *
      8  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      9  1.1  tsutsui  * modification, are permitted provided that the following conditions
     10  1.1  tsutsui  * are met:
     11  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     12  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     13  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     16  1.1  tsutsui  * 3. All advertising materials mentioning features or use of this software
     17  1.1  tsutsui  *    must display the following acknowledgement:
     18  1.1  tsutsui  *	This product includes software developed by TooLs GmbH.
     19  1.1  tsutsui  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  1.1  tsutsui  *    derived from this software without specific prior written permission.
     21  1.1  tsutsui  *
     22  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  tsutsui  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  1.1  tsutsui  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  1.1  tsutsui  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  1.1  tsutsui  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.1  tsutsui  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  1.1  tsutsui  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  1.1  tsutsui  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  tsutsui  */
     33  1.1  tsutsui /*
     34  1.1  tsutsui  * romcons.c - from sys/dev/ofw/ofcons.c
     35  1.1  tsutsui  */
     36  1.1  tsutsui 
     37  1.1  tsutsui #include <sys/cdefs.h>
     38  1.1  tsutsui __KERNEL_RCSID(0, "$NetBSD: romcons.c,v 1.1 2011/11/20 15:38:00 tsutsui Exp $");
     39  1.1  tsutsui 
     40  1.1  tsutsui #include <sys/param.h>
     41  1.1  tsutsui #include <sys/conf.h>
     42  1.1  tsutsui #include <sys/device.h>
     43  1.1  tsutsui #include <sys/proc.h>
     44  1.1  tsutsui #include <sys/systm.h>
     45  1.1  tsutsui #include <sys/callout.h>
     46  1.1  tsutsui #include <sys/tty.h>
     47  1.1  tsutsui #include <sys/kauth.h>
     48  1.1  tsutsui 
     49  1.1  tsutsui #include <dev/cons.h>
     50  1.1  tsutsui 
     51  1.1  tsutsui #include <machine/autoconf.h>
     52  1.1  tsutsui #include <machine/romcall.h>
     53  1.1  tsutsui 
     54  1.1  tsutsui #include "ioconf.h"
     55  1.1  tsutsui 
     56  1.1  tsutsui struct romcons_softc {
     57  1.1  tsutsui 	device_t sc_dev;
     58  1.1  tsutsui 	struct tty *sc_tty;
     59  1.1  tsutsui 	struct callout sc_poll_ch;
     60  1.1  tsutsui 	int sc_flags;
     61  1.1  tsutsui #define	CONS_POLL	1
     62  1.1  tsutsui };
     63  1.1  tsutsui 
     64  1.1  tsutsui #define	BURSTLEN	128	/* max number of bytes to write in one chunk */
     65  1.1  tsutsui 
     66  1.1  tsutsui cons_decl(romcons_);
     67  1.1  tsutsui 
     68  1.1  tsutsui static int romcons_match(device_t, cfdata_t, void *);
     69  1.1  tsutsui static void romcons_attach(device_t, device_t, void *);
     70  1.1  tsutsui 
     71  1.1  tsutsui CFATTACH_DECL_NEW(romcons, sizeof(struct romcons_softc),
     72  1.1  tsutsui     romcons_match, romcons_attach, NULL, NULL);
     73  1.1  tsutsui 
     74  1.1  tsutsui dev_type_open(romcons_open);
     75  1.1  tsutsui dev_type_close(romcons_close);
     76  1.1  tsutsui dev_type_read(romcons_read);
     77  1.1  tsutsui dev_type_write(romcons_write);
     78  1.1  tsutsui dev_type_ioctl(romcons_ioctl);
     79  1.1  tsutsui dev_type_tty(romcons_tty);
     80  1.1  tsutsui dev_type_poll(romcons_poll);
     81  1.1  tsutsui 
     82  1.1  tsutsui void romcons_kbdinput(int);
     83  1.1  tsutsui 
     84  1.1  tsutsui const struct cdevsw romcons_cdevsw = {
     85  1.1  tsutsui 	romcons_open, romcons_close, romcons_read, romcons_write, romcons_ioctl,
     86  1.1  tsutsui 	nostop, romcons_tty, romcons_poll, nommap, ttykqfilter, D_TTY
     87  1.1  tsutsui };
     88  1.1  tsutsui 
     89  1.1  tsutsui struct consdev consdev_rom = cons_init(romcons_);
     90  1.1  tsutsui 
     91  1.1  tsutsui bool romcons_is_console;
     92  1.1  tsutsui 
     93  1.1  tsutsui static int
     94  1.1  tsutsui romcons_match(device_t parent, cfdata_t match, void *aux)
     95  1.1  tsutsui {
     96  1.1  tsutsui 	struct mainbus_attach_args *ma = aux;
     97  1.1  tsutsui 	static bool romcons_matched;
     98  1.1  tsutsui 
     99  1.1  tsutsui 	if (strcmp(ma->ma_name, "romcons"))
    100  1.1  tsutsui 		return 0;
    101  1.1  tsutsui 
    102  1.1  tsutsui 	if (!romcons_is_console)
    103  1.1  tsutsui 		return 0;
    104  1.1  tsutsui 
    105  1.1  tsutsui 	if (romcons_matched)
    106  1.1  tsutsui 		return 0;
    107  1.1  tsutsui 
    108  1.1  tsutsui 	romcons_matched = true;
    109  1.1  tsutsui 	return 1;
    110  1.1  tsutsui }
    111  1.1  tsutsui 
    112  1.1  tsutsui static void
    113  1.1  tsutsui romcons_attach(device_t parent, device_t self, void *aux)
    114  1.1  tsutsui {
    115  1.1  tsutsui 	struct romcons_softc *sc = device_private(self);
    116  1.1  tsutsui 
    117  1.1  tsutsui 	sc->sc_dev = self;
    118  1.1  tsutsui 	vectab[46] = romcallvec;	/* XXX */
    119  1.1  tsutsui 	aprint_normal("\n");
    120  1.1  tsutsui 
    121  1.1  tsutsui 	callout_init(&sc->sc_poll_ch, 0);
    122  1.1  tsutsui }
    123  1.1  tsutsui 
    124  1.1  tsutsui static void romcons_start(struct tty *);
    125  1.1  tsutsui static int romcons_param(struct tty *, struct termios *);
    126  1.1  tsutsui static void romcons_pollin(void *);
    127  1.1  tsutsui 
    128  1.1  tsutsui int
    129  1.1  tsutsui romcons_open(dev_t dev, int flag, int mode, struct lwp *l)
    130  1.1  tsutsui {
    131  1.1  tsutsui 	struct romcons_softc *sc;
    132  1.1  tsutsui 	struct tty *tp;
    133  1.1  tsutsui 
    134  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    135  1.1  tsutsui 	if (sc == NULL)
    136  1.1  tsutsui 		return ENXIO;
    137  1.1  tsutsui 	if ((tp = sc->sc_tty) == 0)
    138  1.1  tsutsui 		sc->sc_tty = tp = tty_alloc();
    139  1.1  tsutsui 	tp->t_oproc = romcons_start;
    140  1.1  tsutsui 	tp->t_param = romcons_param;
    141  1.1  tsutsui 	tp->t_dev = dev;
    142  1.1  tsutsui 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    143  1.1  tsutsui 		return EBUSY;
    144  1.1  tsutsui 	if ((tp->t_state & TS_ISOPEN) == 0) {
    145  1.1  tsutsui 		ttychars(tp);
    146  1.1  tsutsui 		tp->t_iflag = TTYDEF_IFLAG;
    147  1.1  tsutsui 		tp->t_oflag = TTYDEF_OFLAG;
    148  1.1  tsutsui 		tp->t_cflag = TTYDEF_CFLAG;
    149  1.1  tsutsui 		tp->t_lflag = TTYDEF_LFLAG;
    150  1.1  tsutsui 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    151  1.1  tsutsui 		romcons_param(tp, &tp->t_termios);
    152  1.1  tsutsui 		ttsetwater(tp);
    153  1.1  tsutsui 	}
    154  1.1  tsutsui 	tp->t_state |= TS_CARR_ON;
    155  1.1  tsutsui 
    156  1.1  tsutsui 	if ((sc->sc_flags & CONS_POLL) == 0) {
    157  1.1  tsutsui 		sc->sc_flags |= CONS_POLL;
    158  1.1  tsutsui 		callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
    159  1.1  tsutsui 	}
    160  1.1  tsutsui 
    161  1.1  tsutsui 	return (*tp->t_linesw->l_open)(dev, tp);
    162  1.1  tsutsui }
    163  1.1  tsutsui 
    164  1.1  tsutsui int
    165  1.1  tsutsui romcons_close(dev_t dev, int flag, int mode, struct lwp *l)
    166  1.1  tsutsui {
    167  1.1  tsutsui 	struct romcons_softc *sc;
    168  1.1  tsutsui 	struct tty *tp;
    169  1.1  tsutsui 
    170  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    171  1.1  tsutsui 	tp = sc->sc_tty;
    172  1.1  tsutsui 	callout_stop(&sc->sc_poll_ch);
    173  1.1  tsutsui 	sc->sc_flags &= ~CONS_POLL;
    174  1.1  tsutsui 	(*tp->t_linesw->l_close)(tp, flag);
    175  1.1  tsutsui 	ttyclose(tp);
    176  1.1  tsutsui 	return 0;
    177  1.1  tsutsui }
    178  1.1  tsutsui 
    179  1.1  tsutsui int
    180  1.1  tsutsui romcons_read(dev_t dev, struct uio *uio, int flag)
    181  1.1  tsutsui {
    182  1.1  tsutsui 	struct romcons_softc *sc;
    183  1.1  tsutsui 	struct tty *tp;
    184  1.1  tsutsui 
    185  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    186  1.1  tsutsui 	tp = sc->sc_tty;
    187  1.1  tsutsui 
    188  1.1  tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    189  1.1  tsutsui }
    190  1.1  tsutsui 
    191  1.1  tsutsui int
    192  1.1  tsutsui romcons_write(dev_t dev, struct uio *uio, int flag)
    193  1.1  tsutsui {
    194  1.1  tsutsui 	struct romcons_softc *sc;
    195  1.1  tsutsui 	struct tty *tp;
    196  1.1  tsutsui 
    197  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    198  1.1  tsutsui 	tp = sc->sc_tty;
    199  1.1  tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    200  1.1  tsutsui }
    201  1.1  tsutsui 
    202  1.1  tsutsui int
    203  1.1  tsutsui romcons_poll(dev_t dev, int events, struct lwp *l)
    204  1.1  tsutsui {
    205  1.1  tsutsui 	struct romcons_softc *sc;
    206  1.1  tsutsui 	struct tty *tp;
    207  1.1  tsutsui 
    208  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    209  1.1  tsutsui 	tp = sc->sc_tty;
    210  1.1  tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    211  1.1  tsutsui }
    212  1.1  tsutsui int
    213  1.1  tsutsui romcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    214  1.1  tsutsui {
    215  1.1  tsutsui 	struct romcons_softc *sc;
    216  1.1  tsutsui 	struct tty *tp;
    217  1.1  tsutsui 	int error;
    218  1.1  tsutsui 
    219  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    220  1.1  tsutsui 	tp = sc->sc_tty;
    221  1.1  tsutsui 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) !=
    222  1.1  tsutsui 	    EPASSTHROUGH)
    223  1.1  tsutsui 		return error;
    224  1.1  tsutsui 	return ttioctl(tp, cmd, data, flag, l);
    225  1.1  tsutsui }
    226  1.1  tsutsui 
    227  1.1  tsutsui struct tty *
    228  1.1  tsutsui romcons_tty(dev_t dev)
    229  1.1  tsutsui {
    230  1.1  tsutsui 	struct romcons_softc *sc;
    231  1.1  tsutsui 
    232  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    233  1.1  tsutsui 	return sc->sc_tty;
    234  1.1  tsutsui }
    235  1.1  tsutsui 
    236  1.1  tsutsui static void
    237  1.1  tsutsui romcons_start(struct tty *tp)
    238  1.1  tsutsui {
    239  1.1  tsutsui 	int s, len;
    240  1.1  tsutsui 	uint8_t buf[BURSTLEN];
    241  1.1  tsutsui 
    242  1.1  tsutsui 	s = spltty();
    243  1.1  tsutsui 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    244  1.1  tsutsui 		splx(s);
    245  1.1  tsutsui 		return;
    246  1.1  tsutsui 	}
    247  1.1  tsutsui 	tp->t_state |= TS_BUSY;
    248  1.1  tsutsui 	splx(s);
    249  1.1  tsutsui 	len = q_to_b(&tp->t_outq, buf, BURSTLEN);
    250  1.1  tsutsui 	s = splhigh();
    251  1.1  tsutsui 	rom_write(1, buf, len);
    252  1.1  tsutsui 	splx(s);
    253  1.1  tsutsui 	s = spltty();
    254  1.1  tsutsui 	tp->t_state &= ~TS_BUSY;
    255  1.1  tsutsui 	if (ttypull(tp)) {
    256  1.1  tsutsui 		tp->t_state |= TS_TIMEOUT;
    257  1.1  tsutsui 		callout_schedule(&tp->t_rstrt_ch, 1);
    258  1.1  tsutsui 	}
    259  1.1  tsutsui 	splx(s);
    260  1.1  tsutsui }
    261  1.1  tsutsui 
    262  1.1  tsutsui static int
    263  1.1  tsutsui romcons_param(struct tty *tp, struct termios *t)
    264  1.1  tsutsui {
    265  1.1  tsutsui 
    266  1.1  tsutsui 	tp->t_ispeed = t->c_ispeed;
    267  1.1  tsutsui 	tp->t_ospeed = t->c_ospeed;
    268  1.1  tsutsui 	tp->t_cflag = t->c_cflag;
    269  1.1  tsutsui 	return 0;
    270  1.1  tsutsui }
    271  1.1  tsutsui 
    272  1.1  tsutsui static void
    273  1.1  tsutsui romcons_pollin(void *aux)
    274  1.1  tsutsui {
    275  1.1  tsutsui 	struct romcons_softc *sc = aux;
    276  1.1  tsutsui 	struct tty *tp = sc->sc_tty;
    277  1.1  tsutsui 	char ch;
    278  1.1  tsutsui 	int rv;
    279  1.1  tsutsui 
    280  1.1  tsutsui 	while (0 && (rv = rom_read(1, &ch, 1)) > 0) {
    281  1.1  tsutsui 		if (tp && (tp->t_state & TS_ISOPEN))
    282  1.1  tsutsui 			(*tp->t_linesw->l_rint)(ch, tp);
    283  1.1  tsutsui 	}
    284  1.1  tsutsui 	callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
    285  1.1  tsutsui }
    286  1.1  tsutsui 
    287  1.1  tsutsui void
    288  1.1  tsutsui romcons_kbdinput(int ks)
    289  1.1  tsutsui {
    290  1.1  tsutsui 	struct romcons_softc *sc;
    291  1.1  tsutsui 	struct tty *tp;
    292  1.1  tsutsui 
    293  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, 0);
    294  1.1  tsutsui 	tp = sc->sc_tty;
    295  1.1  tsutsui 	if (tp && (tp->t_state & TS_ISOPEN))
    296  1.1  tsutsui 		(*tp->t_linesw->l_rint)(ks, tp);
    297  1.1  tsutsui }
    298  1.1  tsutsui 
    299  1.1  tsutsui void
    300  1.1  tsutsui romcons_cnprobe(struct consdev *cd)
    301  1.1  tsutsui {
    302  1.1  tsutsui }
    303  1.1  tsutsui 
    304  1.1  tsutsui void
    305  1.1  tsutsui romcons_cninit(struct consdev *cd)
    306  1.1  tsutsui {
    307  1.1  tsutsui 	int maj;
    308  1.1  tsutsui 
    309  1.1  tsutsui 	maj = cdevsw_lookup_major(&romcons_cdevsw);
    310  1.1  tsutsui 	cd->cn_dev = makedev(maj, 0);
    311  1.1  tsutsui 	romcons_is_console = true;
    312  1.1  tsutsui 	vectab[46] = romcallvec;	/* XXX */
    313  1.1  tsutsui }
    314  1.1  tsutsui 
    315  1.1  tsutsui int
    316  1.1  tsutsui romcons_cngetc(dev_t dev)
    317  1.1  tsutsui {
    318  1.1  tsutsui 	unsigned char ch = '\0';
    319  1.1  tsutsui 	int l;
    320  1.1  tsutsui 
    321  1.1  tsutsui 	while ((l = rom_read(1, &ch, 1)) != 1)
    322  1.1  tsutsui 		if (l != -2 && l != 0)
    323  1.1  tsutsui 			return -1;
    324  1.1  tsutsui 	return ch;
    325  1.1  tsutsui }
    326  1.1  tsutsui 
    327  1.1  tsutsui void
    328  1.1  tsutsui romcons_cnputc(dev_t dev, int c)
    329  1.1  tsutsui {
    330  1.1  tsutsui 	char ch = c;
    331  1.1  tsutsui 
    332  1.1  tsutsui 	rom_write(1, &ch, 1);
    333  1.1  tsutsui }
    334  1.1  tsutsui 
    335  1.1  tsutsui void
    336  1.1  tsutsui romcons_cnpollc(dev_t dev, int on)
    337  1.1  tsutsui {
    338  1.1  tsutsui 	struct romcons_softc *sc;
    339  1.1  tsutsui 
    340  1.1  tsutsui 	sc = device_lookup_private(&romcons_cd, minor(dev));
    341  1.1  tsutsui 
    342  1.1  tsutsui 	if (sc == NULL)
    343  1.1  tsutsui 		return;
    344  1.1  tsutsui 	if (on) {
    345  1.1  tsutsui 		if (sc->sc_flags & CONS_POLL)
    346  1.1  tsutsui 			callout_stop(&sc->sc_poll_ch);
    347  1.1  tsutsui 		sc->sc_flags &= ~CONS_POLL;
    348  1.1  tsutsui 	} else {
    349  1.1  tsutsui 		if ((sc->sc_flags & CONS_POLL) == 0) {
    350  1.1  tsutsui 			sc->sc_flags |= CONS_POLL;
    351  1.1  tsutsui 			callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc);
    352  1.1  tsutsui 		}
    353  1.1  tsutsui 	}
    354  1.1  tsutsui }
    355