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