Home | History | Annotate | Line # | Download | only in dev
ttycons.c revision 1.11
      1  1.11  jmcneill /* $NetBSD: ttycons.c,v 1.11 2011/12/12 17:07:42 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.11  jmcneill __KERNEL_RCSID(0, "$NetBSD: ttycons.c,v 1.11 2011/12/12 17:07:42 jmcneill Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.8  jmcneill #include <sys/conf.h>
     34   1.1  jmcneill #include <sys/proc.h>
     35   1.1  jmcneill #include <sys/systm.h>
     36   1.1  jmcneill #include <sys/device.h>
     37   1.8  jmcneill #include <sys/kauth.h>
     38   1.5  jmcneill #include <sys/termios.h>
     39   1.8  jmcneill #include <sys/tty.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/cons.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <machine/mainbus.h>
     44   1.4  jmcneill #include <machine/thunk.h>
     45   1.1  jmcneill 
     46   1.1  jmcneill static int	ttycons_match(device_t, cfdata_t, void *);
     47   1.1  jmcneill static void	ttycons_attach(device_t, device_t, void *);
     48   1.1  jmcneill 
     49   1.1  jmcneill void		ttycons_consinit(void);
     50   1.1  jmcneill 
     51   1.8  jmcneill struct ttycons_softc {
     52   1.1  jmcneill 	device_t	sc_dev;
     53   1.8  jmcneill 	struct tty	*sc_tty;
     54   1.8  jmcneill 	void		*sc_rd_sih;
     55  1.11  jmcneill 	void		*sc_ctrlc_sih;
     56   1.8  jmcneill };
     57   1.1  jmcneill 
     58   1.1  jmcneill dev_type_cngetc(ttycons_cngetc);
     59   1.1  jmcneill dev_type_cnputc(ttycons_cnputc);
     60   1.1  jmcneill dev_type_cnpollc(ttycons_cnpollc);
     61   1.1  jmcneill 
     62   1.1  jmcneill static struct cnm_state ttycons_cnm_state;
     63   1.1  jmcneill struct consdev ttycons_consdev = {
     64   1.1  jmcneill 	.cn_getc = ttycons_cngetc,
     65   1.1  jmcneill 	.cn_putc = ttycons_cnputc,
     66   1.1  jmcneill 	.cn_pollc = ttycons_cnpollc,
     67   1.7  jmcneill 	.cn_dev = NODEV,
     68   1.7  jmcneill 	.cn_pri = CN_NORMAL,
     69   1.1  jmcneill };
     70   1.1  jmcneill 
     71   1.8  jmcneill CFATTACH_DECL_NEW(ttycons, sizeof(struct ttycons_softc),
     72   1.1  jmcneill     ttycons_match, ttycons_attach, NULL, NULL);
     73   1.1  jmcneill 
     74   1.8  jmcneill extern struct cfdriver ttycons_cd;
     75   1.8  jmcneill 
     76   1.8  jmcneill dev_type_open(ttycons_open);
     77   1.8  jmcneill dev_type_close(ttycons_close);
     78   1.8  jmcneill dev_type_read(ttycons_read);
     79   1.8  jmcneill dev_type_write(ttycons_write);
     80   1.8  jmcneill dev_type_ioctl(ttycons_ioctl);
     81   1.8  jmcneill dev_type_stop(ttycons_stop);
     82   1.8  jmcneill dev_type_tty(ttycons_tty);
     83   1.8  jmcneill dev_type_poll(ttycons_poll);
     84   1.8  jmcneill 
     85   1.8  jmcneill const struct cdevsw ttycons_cdevsw = {
     86   1.8  jmcneill 	.d_open = ttycons_open,
     87   1.8  jmcneill 	.d_close = ttycons_close,
     88   1.8  jmcneill 	.d_read = ttycons_read,
     89   1.8  jmcneill 	.d_write = ttycons_write,
     90   1.8  jmcneill 	.d_ioctl = ttycons_ioctl,
     91   1.8  jmcneill 	.d_stop = ttycons_stop,
     92   1.8  jmcneill 	.d_tty = ttycons_tty,
     93   1.8  jmcneill 	.d_poll = ttycons_poll,
     94   1.8  jmcneill 	.d_kqfilter = ttykqfilter,
     95   1.8  jmcneill 	.d_flag = D_TTY,
     96   1.8  jmcneill };
     97   1.8  jmcneill 
     98   1.8  jmcneill static void	ttycons_start(struct tty *);
     99   1.8  jmcneill static int	ttycons_param(struct tty *, struct termios *);
    100   1.8  jmcneill 
    101   1.8  jmcneill static void	ttycons_intr(int);
    102   1.8  jmcneill static void	ttycons_softintr(void *);
    103   1.8  jmcneill 
    104  1.11  jmcneill static void	ttycons_ctrlc(int);
    105  1.11  jmcneill static void	ttycons_softctrlc(void *);
    106  1.11  jmcneill 
    107   1.1  jmcneill static int
    108   1.1  jmcneill ttycons_match(device_t parent, cfdata_t match, void *opaque)
    109   1.1  jmcneill {
    110   1.1  jmcneill 	struct thunkbus_attach_args *taa = opaque;
    111   1.1  jmcneill 
    112   1.1  jmcneill 	if (taa->taa_type != THUNKBUS_TYPE_TTYCONS)
    113   1.1  jmcneill 		return 0;
    114   1.1  jmcneill 
    115   1.1  jmcneill 	return 1;
    116   1.1  jmcneill }
    117   1.1  jmcneill 
    118   1.1  jmcneill static void
    119   1.1  jmcneill ttycons_attach(device_t parent, device_t self, void *opaque)
    120   1.1  jmcneill {
    121   1.8  jmcneill 	struct ttycons_softc *sc = device_private(self);
    122   1.8  jmcneill 	int maj;
    123   1.1  jmcneill 
    124   1.1  jmcneill 	aprint_naive("\n");
    125   1.1  jmcneill 	aprint_normal(": console\n");
    126   1.1  jmcneill 
    127   1.1  jmcneill 	sc->sc_dev = self;
    128   1.8  jmcneill 	sc->sc_tty = tty_alloc();
    129   1.8  jmcneill 	tty_attach(sc->sc_tty);
    130   1.8  jmcneill 	sc->sc_tty->t_oproc = ttycons_start;
    131   1.8  jmcneill 	sc->sc_tty->t_param = ttycons_param;
    132   1.8  jmcneill 
    133   1.8  jmcneill 	maj = cdevsw_lookup_major(&ttycons_cdevsw);
    134   1.8  jmcneill 	cn_tab->cn_dev = makedev(maj, device_unit(self));
    135   1.8  jmcneill 	sc->sc_tty->t_dev = cn_tab->cn_dev;
    136   1.8  jmcneill 
    137   1.8  jmcneill 	sc->sc_rd_sih = softint_establish(SOFTINT_SERIAL,
    138   1.8  jmcneill 	    ttycons_softintr, sc);
    139   1.8  jmcneill 	if (sc->sc_rd_sih == NULL)
    140  1.11  jmcneill 		panic("couldn't establish ttycons intr handler\n");
    141  1.11  jmcneill 
    142  1.11  jmcneill 	sc->sc_ctrlc_sih = softint_establish(SOFTINT_SERIAL,
    143  1.11  jmcneill 	    ttycons_softctrlc, sc);
    144  1.11  jmcneill 	if (sc->sc_ctrlc_sih == NULL)
    145  1.11  jmcneill 		panic("couldn't establish ttycons ctrlc handler\n");
    146   1.8  jmcneill 
    147   1.8  jmcneill 	thunk_signal(SIGIO, ttycons_intr);
    148  1.11  jmcneill 	thunk_signal(SIGINT, ttycons_ctrlc);
    149  1.10  jmcneill 	if (thunk_set_stdin_sigio(true) != 0)
    150  1.10  jmcneill 		panic("couldn't enable stdin async mode");
    151   1.1  jmcneill }
    152   1.1  jmcneill 
    153   1.1  jmcneill void
    154   1.1  jmcneill ttycons_consinit(void)
    155   1.1  jmcneill {
    156   1.5  jmcneill 	struct thunk_termios t;
    157   1.5  jmcneill 
    158   1.5  jmcneill 	thunk_tcgetattr(0, &t);
    159   1.5  jmcneill 	t.c_lflag &= ~(ECHO|ICANON);
    160   1.5  jmcneill 	t.c_cc[VTIME] = 0;
    161   1.5  jmcneill 	t.c_cc[VMIN] = 1;
    162   1.5  jmcneill 	thunk_tcsetattr(0, TCSANOW, &t);
    163   1.5  jmcneill 
    164   1.1  jmcneill 	cn_tab = &ttycons_consdev;
    165   1.1  jmcneill 	cn_init_magic(&ttycons_cnm_state);
    166   1.1  jmcneill 	cn_set_magic("\047\001");
    167   1.1  jmcneill }
    168   1.1  jmcneill 
    169   1.1  jmcneill int
    170   1.1  jmcneill ttycons_cngetc(dev_t dev)
    171   1.1  jmcneill {
    172   1.4  jmcneill 	return thunk_getchar();
    173   1.1  jmcneill }
    174   1.1  jmcneill 
    175   1.1  jmcneill void
    176   1.1  jmcneill ttycons_cnputc(dev_t dev, int c)
    177   1.1  jmcneill {
    178   1.4  jmcneill 	thunk_putchar(c);
    179   1.1  jmcneill }
    180   1.1  jmcneill 
    181   1.1  jmcneill void
    182   1.1  jmcneill ttycons_cnpollc(dev_t dev, int on)
    183   1.1  jmcneill {
    184   1.1  jmcneill }
    185   1.8  jmcneill 
    186   1.8  jmcneill int
    187   1.8  jmcneill ttycons_open(dev_t dev, int flag, int mode, lwp_t *l)
    188   1.8  jmcneill {
    189   1.8  jmcneill 	struct ttycons_softc *sc;
    190   1.8  jmcneill 	struct tty *t;
    191   1.8  jmcneill 
    192   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    193   1.8  jmcneill 	if (sc == NULL)
    194   1.8  jmcneill 		return ENXIO;
    195   1.8  jmcneill 	t = sc->sc_tty;
    196   1.8  jmcneill 
    197   1.8  jmcneill 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, t))
    198   1.8  jmcneill 		return EBUSY;
    199   1.8  jmcneill 
    200   1.8  jmcneill 	if ((t->t_state & TS_ISOPEN) == 0 && t->t_wopen == 0) {
    201   1.8  jmcneill 		t->t_dev = dev;
    202   1.8  jmcneill 		ttychars(t);
    203   1.8  jmcneill 		t->t_iflag = TTYDEF_IFLAG;
    204   1.8  jmcneill 		t->t_oflag = TTYDEF_OFLAG;
    205   1.8  jmcneill 		t->t_cflag = TTYDEF_CFLAG;
    206   1.8  jmcneill 		t->t_lflag = TTYDEF_LFLAG;
    207   1.8  jmcneill 		t->t_ispeed = t->t_ospeed = TTYDEF_SPEED;
    208   1.8  jmcneill 		ttycons_param(t, &t->t_termios);
    209   1.8  jmcneill 		ttsetwater(t);
    210   1.8  jmcneill 	}
    211   1.8  jmcneill 	t->t_state |= TS_CARR_ON;
    212   1.8  jmcneill 
    213   1.8  jmcneill 	return t->t_linesw->l_open(dev, t);
    214   1.8  jmcneill }
    215   1.8  jmcneill 
    216   1.8  jmcneill int
    217   1.8  jmcneill ttycons_close(dev_t dev, int flag, int mode, lwp_t *l)
    218   1.8  jmcneill {
    219   1.8  jmcneill 	struct ttycons_softc *sc;
    220   1.8  jmcneill 	struct tty *t;
    221   1.8  jmcneill 
    222   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    223   1.8  jmcneill 	t = sc->sc_tty;
    224   1.8  jmcneill 
    225   1.8  jmcneill 	if (t == NULL)
    226   1.8  jmcneill 		return 0;
    227   1.8  jmcneill 
    228   1.8  jmcneill 	t->t_linesw->l_close(t, flag);
    229   1.8  jmcneill 	ttyclose(t);
    230   1.8  jmcneill 
    231   1.8  jmcneill 	return 0;
    232   1.8  jmcneill }
    233   1.8  jmcneill 
    234   1.8  jmcneill int
    235   1.8  jmcneill ttycons_read(dev_t dev, struct uio *uio, int flag)
    236   1.8  jmcneill {
    237   1.8  jmcneill 	struct ttycons_softc *sc;
    238   1.8  jmcneill 	struct tty *t;
    239   1.8  jmcneill 
    240   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    241   1.8  jmcneill 	t = sc->sc_tty;
    242   1.8  jmcneill 
    243   1.8  jmcneill 	return t->t_linesw->l_read(t, uio, flag);
    244   1.8  jmcneill }
    245   1.8  jmcneill 
    246   1.8  jmcneill int
    247   1.8  jmcneill ttycons_write(dev_t dev, struct uio *uio, int flag)
    248   1.8  jmcneill {
    249   1.8  jmcneill 	struct ttycons_softc *sc;
    250   1.8  jmcneill 	struct tty *t;
    251   1.8  jmcneill 
    252   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    253   1.8  jmcneill 	t = sc->sc_tty;
    254   1.8  jmcneill 
    255   1.8  jmcneill 	return t->t_linesw->l_write(t, uio, flag);
    256   1.8  jmcneill }
    257   1.8  jmcneill 
    258   1.8  jmcneill int
    259   1.8  jmcneill ttycons_poll(dev_t dev, int events, lwp_t *l)
    260   1.8  jmcneill {
    261   1.8  jmcneill 	struct ttycons_softc *sc;
    262   1.8  jmcneill 	struct tty *t;
    263   1.8  jmcneill 
    264   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    265   1.8  jmcneill 	t = sc->sc_tty;
    266   1.8  jmcneill 
    267   1.8  jmcneill 	return t->t_linesw->l_poll(t, events, l);
    268   1.8  jmcneill }
    269   1.8  jmcneill 
    270   1.8  jmcneill struct tty *
    271   1.8  jmcneill ttycons_tty(dev_t dev)
    272   1.8  jmcneill {
    273   1.8  jmcneill 	struct ttycons_softc *sc;
    274   1.8  jmcneill 
    275   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    276   1.8  jmcneill 
    277   1.8  jmcneill 	return sc->sc_tty;
    278   1.8  jmcneill }
    279   1.8  jmcneill 
    280   1.8  jmcneill int
    281   1.8  jmcneill ttycons_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    282   1.8  jmcneill {
    283   1.8  jmcneill 	struct ttycons_softc *sc;
    284   1.8  jmcneill 	struct tty *t;
    285   1.8  jmcneill 	int error;
    286   1.8  jmcneill 
    287   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(dev));
    288   1.8  jmcneill 	t = sc->sc_tty;
    289   1.8  jmcneill 
    290   1.8  jmcneill 	error = t->t_linesw->l_ioctl(t, cmd, data, flag, l);
    291   1.8  jmcneill 	if (error != EPASSTHROUGH)
    292   1.8  jmcneill 		return error;
    293   1.8  jmcneill 
    294   1.8  jmcneill 	error = ttioctl(t, cmd, data, flag, l);
    295   1.8  jmcneill 	if (error != EPASSTHROUGH)
    296   1.8  jmcneill 		return error;
    297   1.8  jmcneill 
    298   1.8  jmcneill 	return EPASSTHROUGH;
    299   1.8  jmcneill }
    300   1.8  jmcneill 
    301   1.8  jmcneill static void
    302   1.8  jmcneill ttycons_start(struct tty *t)
    303   1.8  jmcneill {
    304   1.8  jmcneill 	u_char buf[64+1];
    305   1.8  jmcneill 	int s, len, i;
    306   1.8  jmcneill 
    307   1.8  jmcneill 	s = spltty();
    308   1.8  jmcneill 	if (t->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
    309   1.8  jmcneill 		splx(s);
    310   1.8  jmcneill 		return;
    311   1.8  jmcneill 	}
    312   1.8  jmcneill 	t->t_state |= TS_BUSY;
    313   1.8  jmcneill 	splx(s);
    314   1.8  jmcneill 
    315   1.8  jmcneill 	len = q_to_b(&t->t_outq, buf, sizeof(buf) - 1);
    316   1.8  jmcneill 	for (i = 0; i < len; i++) {
    317   1.8  jmcneill 		thunk_putchar(buf[i]);
    318   1.8  jmcneill 	}
    319   1.8  jmcneill 
    320   1.8  jmcneill 	s = spltty();
    321   1.8  jmcneill 	t->t_state &= ~TS_BUSY;
    322   1.8  jmcneill 	if (ttypull(t)) {
    323   1.8  jmcneill 		t->t_state |= TS_TIMEOUT;
    324   1.8  jmcneill 		callout_schedule(&t->t_rstrt_ch, 1);
    325   1.8  jmcneill 	}
    326   1.8  jmcneill 	splx(s);
    327   1.8  jmcneill }
    328   1.8  jmcneill 
    329   1.8  jmcneill void
    330   1.8  jmcneill ttycons_stop(struct tty *t, int flag)
    331   1.8  jmcneill {
    332   1.8  jmcneill }
    333   1.8  jmcneill 
    334   1.8  jmcneill static int
    335   1.8  jmcneill ttycons_param(struct tty *t, struct termios *c)
    336   1.8  jmcneill {
    337   1.8  jmcneill 	t->t_ispeed = c->c_ispeed;
    338   1.8  jmcneill 	t->t_ospeed = c->c_ospeed;
    339   1.8  jmcneill 	t->t_cflag = c->c_cflag;
    340   1.8  jmcneill 	return 0;
    341   1.8  jmcneill }
    342   1.8  jmcneill 
    343   1.8  jmcneill static void
    344   1.8  jmcneill ttycons_intr(int sig)
    345   1.8  jmcneill {
    346   1.8  jmcneill 	struct ttycons_softc *sc;
    347   1.8  jmcneill 
    348   1.9  jmcneill 	curcpu()->ci_idepth++;
    349   1.8  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
    350   1.9  jmcneill 	if (sc) {
    351   1.9  jmcneill 		spl_intr(IPL_SERIAL, softint_schedule, sc->sc_rd_sih);
    352   1.9  jmcneill 	}
    353   1.9  jmcneill 	curcpu()->ci_idepth--;
    354   1.8  jmcneill 
    355   1.8  jmcneill }
    356   1.8  jmcneill 
    357   1.8  jmcneill static void
    358   1.8  jmcneill ttycons_softintr(void *priv)
    359   1.8  jmcneill {
    360   1.8  jmcneill 	struct ttycons_softc *sc = priv;
    361   1.8  jmcneill 	struct tty *t = sc->sc_tty;
    362   1.8  jmcneill 	unsigned char ch;
    363   1.8  jmcneill 	int c;
    364   1.8  jmcneill 
    365   1.8  jmcneill 	while ((c = thunk_pollchar()) >= 0) {
    366   1.8  jmcneill 		ch = (unsigned char)c;
    367   1.8  jmcneill 		cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
    368   1.8  jmcneill 		t->t_linesw->l_rint(ch, t);
    369   1.8  jmcneill 	}
    370   1.8  jmcneill }
    371  1.11  jmcneill 
    372  1.11  jmcneill static void
    373  1.11  jmcneill ttycons_ctrlc(int sig)
    374  1.11  jmcneill {
    375  1.11  jmcneill 	struct ttycons_softc *sc;
    376  1.11  jmcneill 
    377  1.11  jmcneill 	curcpu()->ci_idepth++;
    378  1.11  jmcneill 	sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
    379  1.11  jmcneill 	if (sc) {
    380  1.11  jmcneill 		spl_intr(IPL_SERIAL, softint_schedule, sc->sc_ctrlc_sih);
    381  1.11  jmcneill 	}
    382  1.11  jmcneill 	curcpu()->ci_idepth--;
    383  1.11  jmcneill 
    384  1.11  jmcneill }
    385  1.11  jmcneill 
    386  1.11  jmcneill static void
    387  1.11  jmcneill ttycons_softctrlc(void *priv)
    388  1.11  jmcneill {
    389  1.11  jmcneill 	struct ttycons_softc *sc = priv;
    390  1.11  jmcneill 	struct tty *t = sc->sc_tty;
    391  1.11  jmcneill 	unsigned char ch = 3;	/* ETX */
    392  1.11  jmcneill 
    393  1.11  jmcneill 	cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
    394  1.11  jmcneill 	t->t_linesw->l_rint(ch, t);
    395  1.11  jmcneill }
    396