Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.10
      1 /*	$NetBSD: pcons.c,v 1.10 2002/09/06 13:18:43 gehenna 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 "opt_ddb.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/conf.h>
     41 #include <sys/device.h>
     42 #include <sys/file.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/kernel.h>
     45 #include <sys/proc.h>
     46 #include <sys/tty.h>
     47 #include <sys/time.h>
     48 #include <sys/syslog.h>
     49 
     50 #include <machine/autoconf.h>
     51 #include <machine/openfirm.h>
     52 #include <machine/cpu.h>
     53 #include <machine/eeprom.h>
     54 #include <machine/psl.h>
     55 
     56 #include <dev/cons.h>
     57 
     58 #include <sparc64/dev/cons.h>
     59 
     60 static int pconsmatch __P((struct device *, struct cfdata *, void *));
     61 static void pconsattach __P((struct device *, struct device *, void *));
     62 
     63 struct cfattach pcons_ca = {
     64 	sizeof(struct pconssoftc), pconsmatch, pconsattach
     65 };
     66 
     67 extern struct cfdriver pcons_cd;
     68 
     69 dev_type_open(pconsopen);
     70 dev_type_close(pconsclose);
     71 dev_type_read(pconsread);
     72 dev_type_write(pconswrite);
     73 dev_type_ioctl(pconsioctl);
     74 dev_type_tty(pconstty);
     75 dev_type_poll(pconspoll);
     76 
     77 const struct cdevsw pcons_cdevsw = {
     78 	pconsopen, pconsclose, pconsread, pconswrite, pconsioctl,
     79 	nostop, pconstty, pconspoll, nommap, D_TTY
     80 };
     81 
     82 static struct cnm_state pcons_cnm_state;
     83 
     84 static int pconsprobe __P((void));
     85 extern struct consdev *cn_tab;
     86 
     87 static int
     88 pconsmatch(parent, match, aux)
     89 	struct device *parent;
     90 	struct cfdata *match;
     91 	void *aux;
     92 {
     93 	struct mainbus_attach_args *ma = aux;
     94 	extern int  prom_cngetc __P((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(parent, self, aux)
    104 	struct device *parent, *self;
    105 	void *aux;
    106 {
    107 	struct pconssoftc *sc = (struct pconssoftc *) self;
    108 
    109 	printf("\n");
    110 	if (!pconsprobe())
    111 		return;
    112 
    113 	cn_init_magic(&pcons_cnm_state);
    114 	cn_set_magic("+++++");
    115 	callout_init(&sc->sc_poll_ch);
    116 }
    117 
    118 static void pconsstart __P((struct tty *));
    119 static int pconsparam __P((struct tty *, struct termios *));
    120 static void pcons_poll __P((void *));
    121 
    122 int
    123 pconsopen(dev, flag, mode, p)
    124 	dev_t dev;
    125 	int flag, mode;
    126 	struct proc *p;
    127 {
    128 	struct pconssoftc *sc;
    129 	int unit = minor(dev);
    130 	struct tty *tp;
    131 
    132 	if (unit >= pcons_cd.cd_ndevs)
    133 		return ENXIO;
    134 	sc = pcons_cd.cd_devs[unit];
    135 	if (!sc)
    136 		return ENXIO;
    137 	if (!(tp = sc->of_tty))
    138 		sc->of_tty = tp = ttymalloc();
    139 	tp->t_oproc = pconsstart;
    140 	tp->t_param = pconsparam;
    141 	tp->t_dev = dev;
    142 	cn_tab->cn_dev = dev;
    143 	if (!(tp->t_state & TS_ISOPEN)) {
    144 		ttychars(tp);
    145 		tp->t_iflag = TTYDEF_IFLAG;
    146 		tp->t_oflag = TTYDEF_OFLAG;
    147 		tp->t_cflag = TTYDEF_CFLAG;
    148 		tp->t_lflag = TTYDEF_LFLAG;
    149 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    150 		pconsparam(tp, &tp->t_termios);
    151 		ttsetwater(tp);
    152 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
    153 		return EBUSY;
    154 	tp->t_state |= TS_CARR_ON;
    155 
    156 	if (!(sc->of_flags & OFPOLL)) {
    157 		sc->of_flags |= OFPOLL;
    158 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    159 	}
    160 
    161 	return (*tp->t_linesw->l_open)(dev, tp);
    162 }
    163 
    164 int
    165 pconsclose(dev, flag, mode, p)
    166 	dev_t dev;
    167 	int flag, mode;
    168 	struct proc *p;
    169 {
    170 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    171 	struct tty *tp = sc->of_tty;
    172 
    173 	callout_stop(&sc->sc_poll_ch);
    174 	sc->of_flags &= ~OFPOLL;
    175 	(*tp->t_linesw->l_close)(tp, flag);
    176 	ttyclose(tp);
    177 	return 0;
    178 }
    179 
    180 int
    181 pconsread(dev, uio, flag)
    182 	dev_t dev;
    183 	struct uio *uio;
    184 	int flag;
    185 {
    186 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    187 	struct tty *tp = sc->of_tty;
    188 
    189 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    190 }
    191 
    192 int
    193 pconswrite(dev, uio, flag)
    194 	dev_t dev;
    195 	struct uio *uio;
    196 	int flag;
    197 {
    198 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    199 	struct tty *tp = sc->of_tty;
    200 
    201 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    202 }
    203 
    204 int
    205 pconspoll(dev, events, p)
    206 	dev_t dev;
    207 	int events;
    208 	struct proc *p;
    209 {
    210 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    211 	struct tty *tp = sc->of_tty;
    212 
    213 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    214 }
    215 
    216 int
    217 pconsioctl(dev, cmd, data, flag, p)
    218 	dev_t dev;
    219 	u_long cmd;
    220 	caddr_t data;
    221 	int flag;
    222 	struct proc *p;
    223 {
    224 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    225 	struct tty *tp = sc->of_tty;
    226 	int error;
    227 
    228 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) != EPASSTHROUGH)
    229 		return error;
    230 	return ttioctl(tp, cmd, data, flag, p);
    231 }
    232 
    233 struct tty *
    234 pconstty(dev)
    235 	dev_t dev;
    236 {
    237 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    238 
    239 	return sc->of_tty;
    240 }
    241 
    242 static void
    243 pconsstart(tp)
    244 	struct tty *tp;
    245 {
    246 	struct clist *cl;
    247 	int s, len;
    248 	u_char buf[OFBURSTLEN];
    249 
    250 	s = spltty();
    251 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    252 		splx(s);
    253 		return;
    254 	}
    255 	tp->t_state |= TS_BUSY;
    256 	splx(s);
    257 	cl = &tp->t_outq;
    258 	len = q_to_b(cl, buf, OFBURSTLEN);
    259 	OF_write(stdout, buf, len);
    260 	s = spltty();
    261 	tp->t_state &= ~TS_BUSY;
    262 	if (cl->c_cc) {
    263 		tp->t_state |= TS_TIMEOUT;
    264 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    265 	}
    266 	if (cl->c_cc <= tp->t_lowat) {
    267 		if (tp->t_state & TS_ASLEEP) {
    268 			tp->t_state &= ~TS_ASLEEP;
    269 			wakeup(cl);
    270 		}
    271 		selwakeup(&tp->t_wsel);
    272 	}
    273 	splx(s);
    274 }
    275 
    276 static int
    277 pconsparam(tp, t)
    278 	struct tty *tp;
    279 	struct termios *t;
    280 {
    281 	tp->t_ispeed = t->c_ispeed;
    282 	tp->t_ospeed = t->c_ospeed;
    283 	tp->t_cflag = t->c_cflag;
    284 	return 0;
    285 }
    286 
    287 static void
    288 pcons_poll(aux)
    289 	void *aux;
    290 {
    291 	struct pconssoftc *sc = aux;
    292 	struct tty *tp = sc->of_tty;
    293 	char ch;
    294 
    295 	while (OF_read(stdin, &ch, 1) > 0) {
    296 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
    297 		if (tp && (tp->t_state & TS_ISOPEN))
    298 			(*tp->t_linesw->l_rint)(ch, tp);
    299 	}
    300 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    301 }
    302 
    303 int
    304 pconsprobe()
    305 {
    306 	if (!stdin) stdin = OF_stdin();
    307 	if (!stdout) stdout = OF_stdout();
    308 
    309 	return (stdin && stdout);
    310 }
    311 
    312 void
    313 pcons_cnpollc(dev, on)
    314 	dev_t dev;
    315 	int on;
    316 {
    317 	struct pconssoftc *sc = NULL;
    318 
    319 	if (pcons_cd.cd_devs)
    320 		sc = pcons_cd.cd_devs[minor(dev)];
    321 
    322 	if (on) {
    323 		if (!sc) return;
    324 		if (sc->of_flags & OFPOLL)
    325 			callout_stop(&sc->sc_poll_ch);
    326 		sc->of_flags &= ~OFPOLL;
    327 	} else {
    328                 /* Resuming kernel. */
    329 		if (sc && !(sc->of_flags & OFPOLL)) {
    330 			sc->of_flags |= OFPOLL;
    331 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    332 		}
    333 	}
    334 }
    335 
    336 void pcons_dopoll __P((void));
    337 void
    338 pcons_dopoll() {
    339 		pcons_poll((void*)pcons_cd.cd_devs[0]);
    340 }
    341