Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.31
      1 /*	$NetBSD: pcons.c,v 1.31 2011/06/03 03:20:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Eduardo E. Horvath
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Default console driver.  Uses the PROM or whatever
     33  * driver(s) are appropriate.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: pcons.c,v 1.31 2011/06/03 03:20:39 christos Exp $");
     38 
     39 #include "opt_ddb.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/conf.h>
     44 #include <sys/device.h>
     45 #include <sys/file.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/kernel.h>
     48 #include <sys/proc.h>
     49 #include <sys/tty.h>
     50 #include <sys/time.h>
     51 #include <sys/syslog.h>
     52 #include <sys/kauth.h>
     53 
     54 #include <machine/autoconf.h>
     55 #include <machine/openfirm.h>
     56 #include <machine/cpu.h>
     57 #include <machine/eeprom.h>
     58 #include <machine/psl.h>
     59 
     60 #include <dev/cons.h>
     61 
     62 #include <sparc64/dev/cons.h>
     63 
     64 static int pconsmatch(device_t, cfdata_t, void *);
     65 static void pconsattach(device_t, device_t, void *);
     66 
     67 CFATTACH_DECL_NEW(pcons, sizeof(struct pconssoftc),
     68     pconsmatch, pconsattach, NULL, NULL);
     69 
     70 extern struct cfdriver pcons_cd;
     71 
     72 dev_type_open(pconsopen);
     73 dev_type_close(pconsclose);
     74 dev_type_read(pconsread);
     75 dev_type_write(pconswrite);
     76 dev_type_ioctl(pconsioctl);
     77 dev_type_tty(pconstty);
     78 dev_type_poll(pconspoll);
     79 
     80 const struct cdevsw pcons_cdevsw = {
     81 	pconsopen, pconsclose, pconsread, pconswrite, pconsioctl,
     82 	nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY
     83 };
     84 
     85 static struct cnm_state pcons_cnm_state;
     86 
     87 static int pconsprobe(void);
     88 extern struct consdev *cn_tab;
     89 
     90 static int
     91 pconsmatch(device_t parent, cfdata_t match, void *aux)
     92 {
     93 	struct mainbus_attach_args *ma = aux;
     94 	extern int  prom_cngetc(dev_t);
     95 
     96 	/* Only attach if no other console has attached. */
     97 	return ((strcmp("pcons", ma->ma_name) == 0) &&
     98 		(cn_tab->cn_getc == prom_cngetc));
     99 
    100 }
    101 
    102 static void
    103 pconsattach(device_t parent, device_t self, void *aux)
    104 {
    105 	struct pconssoftc *sc = device_private(self);
    106 	sc->of_dev = self;
    107 
    108 	printf("\n");
    109 	if (!pconsprobe())
    110 		return;
    111 
    112 	cn_init_magic(&pcons_cnm_state);
    113 	cn_set_magic("+++++");
    114 	callout_init(&sc->sc_poll_ch, 0);
    115 }
    116 
    117 static void pconsstart(struct tty *);
    118 static int pconsparam(struct tty *, struct termios *);
    119 static void pcons_poll(void *);
    120 
    121 int
    122 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
    123 {
    124 	struct pconssoftc *sc;
    125 	struct tty *tp;
    126 	struct proc *p;
    127 
    128 	p = l->l_proc;
    129 
    130 	sc = device_lookup_private(&pcons_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 = pconsstart;
    136 	tp->t_param = pconsparam;
    137 	tp->t_dev = dev;
    138 	cn_tab->cn_dev = dev;
    139 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    140 		return (EBUSY);
    141 	if (!(tp->t_state & TS_ISOPEN)) {
    142 		ttychars(tp);
    143 		tp->t_iflag = TTYDEF_IFLAG;
    144 		tp->t_oflag = TTYDEF_OFLAG;
    145 		tp->t_cflag = TTYDEF_CFLAG;
    146 		tp->t_lflag = TTYDEF_LFLAG;
    147 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    148 		pconsparam(tp, &tp->t_termios);
    149 		ttsetwater(tp);
    150 	}
    151 	tp->t_state |= TS_CARR_ON;
    152 
    153 	if (!(sc->of_flags & OFPOLL)) {
    154 		sc->of_flags |= OFPOLL;
    155 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    156 	}
    157 
    158 	return (*tp->t_linesw->l_open)(dev, tp);
    159 }
    160 
    161 int
    162 pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
    163 {
    164 	struct pconssoftc *sc = device_lookup_private(&pcons_cd,minor(dev));
    165 	struct tty *tp = sc->of_tty;
    166 
    167 	callout_stop(&sc->sc_poll_ch);
    168 	sc->of_flags &= ~OFPOLL;
    169 	(*tp->t_linesw->l_close)(tp, flag);
    170 	ttyclose(tp);
    171 	return 0;
    172 }
    173 
    174 int
    175 pconsread(dev_t dev, struct uio *uio, int flag)
    176 {
    177 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
    178 	struct tty *tp = sc->of_tty;
    179 
    180 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    181 }
    182 
    183 int
    184 pconswrite(dev_t dev, struct uio *uio, int flag)
    185 {
    186 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
    187 	struct tty *tp = sc->of_tty;
    188 
    189 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    190 }
    191 
    192 int
    193 pconspoll(dev_t dev, int events, struct lwp *l)
    194 {
    195 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
    196 	struct tty *tp = sc->of_tty;
    197 
    198 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    199 }
    200 
    201 int
    202 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    203 {
    204 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
    205 	struct tty *tp = sc->of_tty;
    206 	int error;
    207 
    208 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
    209 		return error;
    210 	return ttioctl(tp, cmd, data, flag, l);
    211 }
    212 
    213 struct tty *
    214 pconstty(dev_t dev)
    215 {
    216 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
    217 
    218 	return sc->of_tty;
    219 }
    220 
    221 static void
    222 pconsstart(struct tty *tp)
    223 {
    224 	struct clist *cl;
    225 	int s, len;
    226 	u_char buf[OFBURSTLEN];
    227 
    228 	s = spltty();
    229 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    230 		splx(s);
    231 		return;
    232 	}
    233 	tp->t_state |= TS_BUSY;
    234 	splx(s);
    235 	cl = &tp->t_outq;
    236 	len = q_to_b(cl, buf, OFBURSTLEN);
    237 	prom_write(prom_stdout(), buf, len);
    238 	s = spltty();
    239 	tp->t_state &= ~TS_BUSY;
    240 	if (ttypull(tp)) {
    241 		tp->t_state |= TS_TIMEOUT;
    242 		callout_schedule(&tp->t_rstrt_ch, 1);
    243 	}
    244 	splx(s);
    245 }
    246 
    247 static int
    248 pconsparam(struct tty *tp, struct termios *t)
    249 {
    250 	tp->t_ispeed = t->c_ispeed;
    251 	tp->t_ospeed = t->c_ospeed;
    252 	tp->t_cflag = t->c_cflag;
    253 	return 0;
    254 }
    255 
    256 static void
    257 pcons_poll(void *aux)
    258 {
    259 	struct pconssoftc *sc = aux;
    260 	struct tty *tp = sc->of_tty;
    261 	char ch;
    262 
    263 	while (prom_read(prom_stdin(), &ch, 1) > 0) {
    264 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
    265 		if (tp && (tp->t_state & TS_ISOPEN))
    266 			(*tp->t_linesw->l_rint)(ch, tp);
    267 	}
    268 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    269 }
    270 
    271 int
    272 pconsprobe(void)
    273 {
    274 
    275 	return (prom_stdin() && prom_stdout());
    276 }
    277 
    278 void
    279 pcons_cnpollc(dev_t dev, int on)
    280 {
    281 	struct pconssoftc *sc;
    282 
    283 	sc = device_lookup_private(&pcons_cd, minor(dev));
    284 	if (sc == NULL)
    285 		return;
    286 
    287 	if (on) {
    288 		if (sc->of_flags & OFPOLL)
    289 			callout_stop(&sc->sc_poll_ch);
    290 		sc->of_flags &= ~OFPOLL;
    291 	} else {
    292                 /* Resuming kernel. */
    293 		if (!(sc->of_flags & OFPOLL)) {
    294 			sc->of_flags |= OFPOLL;
    295 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    296 		}
    297 	}
    298 }
    299 
    300 void pcons_dopoll(void);
    301 void
    302 pcons_dopoll(void)
    303 {
    304 		pcons_poll(device_lookup_private(&pcons_cd, 0));
    305 }
    306