xref: /netbsd-src/sys/arch/sparc64/dev/pcons.c (revision f9228f42259a421502f04b1ddb2df91c2ecbc519)
1*f9228f42Sdholland /*	$NetBSD: pcons.c,v 1.34 2014/07/25 08:10:35 dholland Exp $	*/
217c567f9Seeh 
317c567f9Seeh /*-
417c567f9Seeh  * Copyright (c) 2000 Eduardo E. Horvath
517c567f9Seeh  * All rights reserved.
617c567f9Seeh  *
717c567f9Seeh  * Redistribution and use in source and binary forms, with or without
817c567f9Seeh  * modification, are permitted provided that the following conditions
917c567f9Seeh  * are met:
1017c567f9Seeh  * 1. Redistributions of source code must retain the above copyright
1117c567f9Seeh  *    notice, this list of conditions and the following disclaimer.
1217c567f9Seeh  * 2. Redistributions in binary form must reproduce the above copyright
1317c567f9Seeh  *    notice, this list of conditions and the following disclaimer in the
1417c567f9Seeh  *    documentation and/or other materials provided with the distribution.
1517c567f9Seeh  * 3. The name of the author may not be used to endorse or promote products
1617c567f9Seeh  *    derived from this software without specific prior written permission.
1717c567f9Seeh  *
1817c567f9Seeh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1917c567f9Seeh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2017c567f9Seeh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2117c567f9Seeh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2217c567f9Seeh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2317c567f9Seeh  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2417c567f9Seeh  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2517c567f9Seeh  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2617c567f9Seeh  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2717c567f9Seeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2817c567f9Seeh  * SUCH DAMAGE.
2917c567f9Seeh  */
3017c567f9Seeh 
3117c567f9Seeh /*
3217c567f9Seeh  * Default console driver.  Uses the PROM or whatever
3317c567f9Seeh  * driver(s) are appropriate.
3417c567f9Seeh  */
3517c567f9Seeh 
36ed517291Slukem #include <sys/cdefs.h>
37*f9228f42Sdholland __KERNEL_RCSID(0, "$NetBSD: pcons.c,v 1.34 2014/07/25 08:10:35 dholland Exp $");
38ed517291Slukem 
3917c567f9Seeh #include "opt_ddb.h"
4017c567f9Seeh 
4117c567f9Seeh #include <sys/param.h>
4217c567f9Seeh #include <sys/systm.h>
4317c567f9Seeh #include <sys/conf.h>
4417c567f9Seeh #include <sys/device.h>
4517c567f9Seeh #include <sys/file.h>
4617c567f9Seeh #include <sys/ioctl.h>
4717c567f9Seeh #include <sys/kernel.h>
4817c567f9Seeh #include <sys/proc.h>
4917c567f9Seeh #include <sys/tty.h>
5017c567f9Seeh #include <sys/time.h>
5117c567f9Seeh #include <sys/syslog.h>
528ccb6c93Selad #include <sys/kauth.h>
5317c567f9Seeh 
5417c567f9Seeh #include <machine/autoconf.h>
5517c567f9Seeh #include <machine/openfirm.h>
5617c567f9Seeh #include <machine/cpu.h>
5717c567f9Seeh #include <machine/eeprom.h>
5817c567f9Seeh #include <machine/psl.h>
5917c567f9Seeh 
6017c567f9Seeh #include <dev/cons.h>
6117c567f9Seeh 
6217c567f9Seeh #include <sparc64/dev/cons.h>
6317c567f9Seeh 
64817999e9Schristos static int pconsmatch(device_t, cfdata_t, void *);
65817999e9Schristos static void pconsattach(device_t, device_t, void *);
6617c567f9Seeh 
67817999e9Schristos CFATTACH_DECL_NEW(pcons, sizeof(struct pconssoftc),
684bf871a7Sthorpej     pconsmatch, pconsattach, NULL, NULL);
6917c567f9Seeh 
7017c567f9Seeh extern struct cfdriver pcons_cd;
7177a6b82bSgehenna 
7277a6b82bSgehenna dev_type_open(pconsopen);
7377a6b82bSgehenna dev_type_close(pconsclose);
7477a6b82bSgehenna dev_type_read(pconsread);
7577a6b82bSgehenna dev_type_write(pconswrite);
7677a6b82bSgehenna dev_type_ioctl(pconsioctl);
7777a6b82bSgehenna dev_type_tty(pconstty);
7877a6b82bSgehenna dev_type_poll(pconspoll);
7977a6b82bSgehenna 
8077a6b82bSgehenna const struct cdevsw pcons_cdevsw = {
81a68f9396Sdholland 	.d_open = pconsopen,
82a68f9396Sdholland 	.d_close = pconsclose,
83a68f9396Sdholland 	.d_read = pconsread,
84a68f9396Sdholland 	.d_write = pconswrite,
85a68f9396Sdholland 	.d_ioctl = pconsioctl,
86a68f9396Sdholland 	.d_stop = nostop,
87a68f9396Sdholland 	.d_tty = pconstty,
88a68f9396Sdholland 	.d_poll = pconspoll,
89a68f9396Sdholland 	.d_mmap = nommap,
90a68f9396Sdholland 	.d_kqfilter = ttykqfilter,
91*f9228f42Sdholland 	.d_discard = nodiscard,
92a68f9396Sdholland 	.d_flag = D_TTY
9377a6b82bSgehenna };
9477a6b82bSgehenna 
95052afce2Seeh static struct cnm_state pcons_cnm_state;
9617c567f9Seeh 
971a509d61Scdi static int pconsprobe(void);
98052afce2Seeh extern struct consdev *cn_tab;
9917c567f9Seeh 
10017c567f9Seeh static int
pconsmatch(device_t parent,cfdata_t match,void * aux)101817999e9Schristos pconsmatch(device_t parent, cfdata_t match, void *aux)
10217c567f9Seeh {
10317c567f9Seeh 	struct mainbus_attach_args *ma = aux;
1041a509d61Scdi 	extern int  prom_cngetc(dev_t);
10517c567f9Seeh 
10617c567f9Seeh 	/* Only attach if no other console has attached. */
10717c567f9Seeh 	return ((strcmp("pcons", ma->ma_name) == 0) &&
10817c567f9Seeh 		(cn_tab->cn_getc == prom_cngetc));
10917c567f9Seeh 
11017c567f9Seeh }
11117c567f9Seeh 
11217c567f9Seeh static void
pconsattach(device_t parent,device_t self,void * aux)113817999e9Schristos pconsattach(device_t parent, device_t self, void *aux)
11417c567f9Seeh {
115817999e9Schristos 	struct pconssoftc *sc = device_private(self);
116817999e9Schristos 	sc->of_dev = self;
11717c567f9Seeh 
11817c567f9Seeh 	printf("\n");
11917c567f9Seeh 	if (!pconsprobe())
12017c567f9Seeh 		return;
12117c567f9Seeh 
122052afce2Seeh 	cn_init_magic(&pcons_cnm_state);
123052afce2Seeh 	cn_set_magic("+++++");
12488ab7da9Sad 	callout_init(&sc->sc_poll_ch, 0);
12517c567f9Seeh }
12617c567f9Seeh 
1271a509d61Scdi static void pconsstart(struct tty *);
1281a509d61Scdi static int pconsparam(struct tty *, struct termios *);
1291a509d61Scdi static void pcons_poll(void *);
13017c567f9Seeh 
13117c567f9Seeh int
pconsopen(dev_t dev,int flag,int mode,struct lwp * l)1321a509d61Scdi pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
13317c567f9Seeh {
13417c567f9Seeh 	struct pconssoftc *sc;
13517c567f9Seeh 	struct tty *tp;
13617c567f9Seeh 
1372dea63feScegger 	sc = device_lookup_private(&pcons_cd, minor(dev));
13817c567f9Seeh 	if (!sc)
13917c567f9Seeh 		return ENXIO;
14017c567f9Seeh 	if (!(tp = sc->of_tty))
1412626d576Srmind 		sc->of_tty = tp = tty_alloc();
14217c567f9Seeh 	tp->t_oproc = pconsstart;
14317c567f9Seeh 	tp->t_param = pconsparam;
14417c567f9Seeh 	tp->t_dev = dev;
145052afce2Seeh 	cn_tab->cn_dev = dev;
146e8373398Selad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
147e8373398Selad 		return (EBUSY);
14817c567f9Seeh 	if (!(tp->t_state & TS_ISOPEN)) {
14917c567f9Seeh 		ttychars(tp);
15017c567f9Seeh 		tp->t_iflag = TTYDEF_IFLAG;
15117c567f9Seeh 		tp->t_oflag = TTYDEF_OFLAG;
15217c567f9Seeh 		tp->t_cflag = TTYDEF_CFLAG;
15317c567f9Seeh 		tp->t_lflag = TTYDEF_LFLAG;
15417c567f9Seeh 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
15517c567f9Seeh 		pconsparam(tp, &tp->t_termios);
15617c567f9Seeh 		ttsetwater(tp);
157e8373398Selad 	}
15817c567f9Seeh 	tp->t_state |= TS_CARR_ON;
15917c567f9Seeh 
16017c567f9Seeh 	if (!(sc->of_flags & OFPOLL)) {
16117c567f9Seeh 		sc->of_flags |= OFPOLL;
16217c567f9Seeh 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
16317c567f9Seeh 	}
16417c567f9Seeh 
16523a0c490Seeh 	return (*tp->t_linesw->l_open)(dev, tp);
16617c567f9Seeh }
16717c567f9Seeh 
16817c567f9Seeh int
pconsclose(dev_t dev,int flag,int mode,struct lwp * l)1691a509d61Scdi pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
17017c567f9Seeh {
1712dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd,minor(dev));
17217c567f9Seeh 	struct tty *tp = sc->of_tty;
17317c567f9Seeh 
17417c567f9Seeh 	callout_stop(&sc->sc_poll_ch);
17517c567f9Seeh 	sc->of_flags &= ~OFPOLL;
17623a0c490Seeh 	(*tp->t_linesw->l_close)(tp, flag);
17717c567f9Seeh 	ttyclose(tp);
17817c567f9Seeh 	return 0;
17917c567f9Seeh }
18017c567f9Seeh 
18117c567f9Seeh int
pconsread(dev_t dev,struct uio * uio,int flag)1821a509d61Scdi pconsread(dev_t dev, struct uio *uio, int flag)
18317c567f9Seeh {
1842dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
18517c567f9Seeh 	struct tty *tp = sc->of_tty;
18617c567f9Seeh 
18723a0c490Seeh 	return (*tp->t_linesw->l_read)(tp, uio, flag);
18817c567f9Seeh }
18917c567f9Seeh 
19017c567f9Seeh int
pconswrite(dev_t dev,struct uio * uio,int flag)1911a509d61Scdi pconswrite(dev_t dev, struct uio *uio, int flag)
19217c567f9Seeh {
1932dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
19417c567f9Seeh 	struct tty *tp = sc->of_tty;
19517c567f9Seeh 
19623a0c490Seeh 	return (*tp->t_linesw->l_write)(tp, uio, flag);
19717c567f9Seeh }
19817c567f9Seeh 
19917c567f9Seeh int
pconspoll(dev_t dev,int events,struct lwp * l)2001a509d61Scdi pconspoll(dev_t dev, int events, struct lwp *l)
2012963ff5cSscw {
2022dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
2032963ff5cSscw 	struct tty *tp = sc->of_tty;
2042963ff5cSscw 
20595e1ffb1Schristos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
2062963ff5cSscw }
2072963ff5cSscw 
2082963ff5cSscw int
pconsioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)20953524e44Schristos pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
21017c567f9Seeh {
2112dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
21217c567f9Seeh 	struct tty *tp = sc->of_tty;
21317c567f9Seeh 	int error;
21417c567f9Seeh 
21595e1ffb1Schristos 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
21617c567f9Seeh 		return error;
21795e1ffb1Schristos 	return ttioctl(tp, cmd, data, flag, l);
21817c567f9Seeh }
21917c567f9Seeh 
22017c567f9Seeh struct tty *
pconstty(dev_t dev)2211a509d61Scdi pconstty(dev_t dev)
22217c567f9Seeh {
2232dea63feScegger 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
22417c567f9Seeh 
22517c567f9Seeh 	return sc->of_tty;
22617c567f9Seeh }
22717c567f9Seeh 
22817c567f9Seeh static void
pconsstart(struct tty * tp)2291a509d61Scdi pconsstart(struct tty *tp)
23017c567f9Seeh {
23117c567f9Seeh 	struct clist *cl;
23217c567f9Seeh 	int s, len;
23317c567f9Seeh 	u_char buf[OFBURSTLEN];
23417c567f9Seeh 
23517c567f9Seeh 	s = spltty();
23617c567f9Seeh 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
23717c567f9Seeh 		splx(s);
23817c567f9Seeh 		return;
23917c567f9Seeh 	}
24017c567f9Seeh 	tp->t_state |= TS_BUSY;
24117c567f9Seeh 	splx(s);
24217c567f9Seeh 	cl = &tp->t_outq;
24317c567f9Seeh 	len = q_to_b(cl, buf, OFBURSTLEN);
24405a2b230Spk 	prom_write(prom_stdout(), buf, len);
24517c567f9Seeh 	s = spltty();
24617c567f9Seeh 	tp->t_state &= ~TS_BUSY;
247dc26833bSad 	if (ttypull(tp)) {
24817c567f9Seeh 		tp->t_state |= TS_TIMEOUT;
249d238692cSjoerg 		callout_schedule(&tp->t_rstrt_ch, 1);
25017c567f9Seeh 	}
25117c567f9Seeh 	splx(s);
25217c567f9Seeh }
25317c567f9Seeh 
25417c567f9Seeh static int
pconsparam(struct tty * tp,struct termios * t)2551a509d61Scdi pconsparam(struct tty *tp, struct termios *t)
25617c567f9Seeh {
25717c567f9Seeh 	tp->t_ispeed = t->c_ispeed;
25817c567f9Seeh 	tp->t_ospeed = t->c_ospeed;
25917c567f9Seeh 	tp->t_cflag = t->c_cflag;
26017c567f9Seeh 	return 0;
26117c567f9Seeh }
26217c567f9Seeh 
26317c567f9Seeh static void
pcons_poll(void * aux)2641a509d61Scdi pcons_poll(void *aux)
26517c567f9Seeh {
26617c567f9Seeh 	struct pconssoftc *sc = aux;
26717c567f9Seeh 	struct tty *tp = sc->of_tty;
26817c567f9Seeh 	char ch;
26917c567f9Seeh 
27005a2b230Spk 	while (prom_read(prom_stdin(), &ch, 1) > 0) {
271052afce2Seeh 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
27217c567f9Seeh 		if (tp && (tp->t_state & TS_ISOPEN))
27323a0c490Seeh 			(*tp->t_linesw->l_rint)(ch, tp);
27417c567f9Seeh 	}
27517c567f9Seeh 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
27617c567f9Seeh }
27717c567f9Seeh 
27817c567f9Seeh int
pconsprobe(void)2792dea63feScegger pconsprobe(void)
28017c567f9Seeh {
28117c567f9Seeh 
28205a2b230Spk 	return (prom_stdin() && prom_stdout());
28317c567f9Seeh }
28417c567f9Seeh 
28517c567f9Seeh void
pcons_cnpollc(dev_t dev,int on)2861a509d61Scdi pcons_cnpollc(dev_t dev, int on)
28717c567f9Seeh {
2882dea63feScegger 	struct pconssoftc *sc;
28917c567f9Seeh 
2902dea63feScegger 	sc = device_lookup_private(&pcons_cd, minor(dev));
2912dea63feScegger 	if (sc == NULL)
2922dea63feScegger 		return;
29317c567f9Seeh 
29417c567f9Seeh 	if (on) {
29517c567f9Seeh 		if (sc->of_flags & OFPOLL)
29617c567f9Seeh 			callout_stop(&sc->sc_poll_ch);
29717c567f9Seeh 		sc->of_flags &= ~OFPOLL;
29817c567f9Seeh 	} else {
29917c567f9Seeh                 /* Resuming kernel. */
3002dea63feScegger 		if (!(sc->of_flags & OFPOLL)) {
30117c567f9Seeh 			sc->of_flags |= OFPOLL;
30217c567f9Seeh 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
30317c567f9Seeh 		}
30417c567f9Seeh 	}
30517c567f9Seeh }
306f10d7699Seeh 
3071a509d61Scdi void pcons_dopoll(void);
308f10d7699Seeh void
pcons_dopoll(void)3092dea63feScegger pcons_dopoll(void)
3102dea63feScegger {
3112dea63feScegger 		pcons_poll(device_lookup_private(&pcons_cd, 0));
312f10d7699Seeh }
313