Home | History | Annotate | Line # | Download | only in dev
pcons.c revision 1.22
      1 /*	$NetBSD: pcons.c,v 1.22 2006/07/23 22:06:07 ad 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.22 2006/07/23 22:06:07 ad 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(struct device *, struct cfdata *, void *);
     65 static void pconsattach(struct device *, struct device *, void *);
     66 
     67 CFATTACH_DECL(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(struct device *parent, struct cfdata *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(struct device *parent, struct device *self, void *aux)
    104 {
    105 	struct pconssoftc *sc = (struct pconssoftc *) self;
    106 
    107 	printf("\n");
    108 	if (!pconsprobe())
    109 		return;
    110 
    111 	cn_init_magic(&pcons_cnm_state);
    112 	cn_set_magic("+++++");
    113 	callout_init(&sc->sc_poll_ch);
    114 }
    115 
    116 static void pconsstart(struct tty *);
    117 static int pconsparam(struct tty *, struct termios *);
    118 static void pcons_poll(void *);
    119 
    120 int
    121 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
    122 {
    123 	struct pconssoftc *sc;
    124 	int unit = minor(dev);
    125 	struct tty *tp;
    126 	struct proc *p;
    127 
    128 	p = l->l_proc;
    129 
    130 	if (unit >= pcons_cd.cd_ndevs)
    131 		return ENXIO;
    132 	sc = pcons_cd.cd_devs[unit];
    133 	if (!sc)
    134 		return ENXIO;
    135 	if (!(tp = sc->of_tty))
    136 		sc->of_tty = tp = ttymalloc();
    137 	tp->t_oproc = pconsstart;
    138 	tp->t_param = pconsparam;
    139 	tp->t_dev = dev;
    140 	cn_tab->cn_dev = dev;
    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 	} else if ((tp->t_state&TS_XCLUDE) &&
    151 		   kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    152 		   &l->l_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_t dev, int flag, int mode, struct lwp *l)
    166 {
    167 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    168 	struct tty *tp = sc->of_tty;
    169 
    170 	callout_stop(&sc->sc_poll_ch);
    171 	sc->of_flags &= ~OFPOLL;
    172 	(*tp->t_linesw->l_close)(tp, flag);
    173 	ttyclose(tp);
    174 	return 0;
    175 }
    176 
    177 int
    178 pconsread(dev_t dev, struct uio *uio, int flag)
    179 {
    180 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    181 	struct tty *tp = sc->of_tty;
    182 
    183 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    184 }
    185 
    186 int
    187 pconswrite(dev_t dev, struct uio *uio, int flag)
    188 {
    189 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    190 	struct tty *tp = sc->of_tty;
    191 
    192 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    193 }
    194 
    195 int
    196 pconspoll(dev_t dev, int events, struct lwp *l)
    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_poll)(tp, events, l));
    202 }
    203 
    204 int
    205 pconsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    206 {
    207 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    208 	struct tty *tp = sc->of_tty;
    209 	int error;
    210 
    211 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
    212 		return error;
    213 	return ttioctl(tp, cmd, data, flag, l);
    214 }
    215 
    216 struct tty *
    217 pconstty(dev_t dev)
    218 {
    219 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
    220 
    221 	return sc->of_tty;
    222 }
    223 
    224 static void
    225 pconsstart(struct tty *tp)
    226 {
    227 	struct clist *cl;
    228 	int s, len;
    229 	u_char buf[OFBURSTLEN];
    230 
    231 	s = spltty();
    232 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    233 		splx(s);
    234 		return;
    235 	}
    236 	tp->t_state |= TS_BUSY;
    237 	splx(s);
    238 	cl = &tp->t_outq;
    239 	len = q_to_b(cl, buf, OFBURSTLEN);
    240 	prom_write(prom_stdout(), buf, len);
    241 	s = spltty();
    242 	tp->t_state &= ~TS_BUSY;
    243 	if (cl->c_cc) {
    244 		tp->t_state |= TS_TIMEOUT;
    245 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
    246 	}
    247 	if (cl->c_cc <= tp->t_lowat) {
    248 		if (tp->t_state & TS_ASLEEP) {
    249 			tp->t_state &= ~TS_ASLEEP;
    250 			wakeup(cl);
    251 		}
    252 		selwakeup(&tp->t_wsel);
    253 	}
    254 	splx(s);
    255 }
    256 
    257 static int
    258 pconsparam(struct tty *tp, struct termios *t)
    259 {
    260 	tp->t_ispeed = t->c_ispeed;
    261 	tp->t_ospeed = t->c_ospeed;
    262 	tp->t_cflag = t->c_cflag;
    263 	return 0;
    264 }
    265 
    266 static void
    267 pcons_poll(void *aux)
    268 {
    269 	struct pconssoftc *sc = aux;
    270 	struct tty *tp = sc->of_tty;
    271 	char ch;
    272 
    273 	while (prom_read(prom_stdin(), &ch, 1) > 0) {
    274 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
    275 		if (tp && (tp->t_state & TS_ISOPEN))
    276 			(*tp->t_linesw->l_rint)(ch, tp);
    277 	}
    278 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    279 }
    280 
    281 int
    282 pconsprobe()
    283 {
    284 
    285 	return (prom_stdin() && prom_stdout());
    286 }
    287 
    288 void
    289 pcons_cnpollc(dev_t dev, int on)
    290 {
    291 	struct pconssoftc *sc = NULL;
    292 
    293 	if (pcons_cd.cd_devs)
    294 		sc = pcons_cd.cd_devs[minor(dev)];
    295 
    296 	if (on) {
    297 		if (!sc) return;
    298 		if (sc->of_flags & OFPOLL)
    299 			callout_stop(&sc->sc_poll_ch);
    300 		sc->of_flags &= ~OFPOLL;
    301 	} else {
    302                 /* Resuming kernel. */
    303 		if (sc && !(sc->of_flags & OFPOLL)) {
    304 			sc->of_flags |= OFPOLL;
    305 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
    306 		}
    307 	}
    308 }
    309 
    310 void pcons_dopoll(void);
    311 void
    312 pcons_dopoll() {
    313 		pcons_poll((void*)pcons_cd.cd_devs[0]);
    314 }
    315