xref: /netbsd-src/sys/arch/sparc64/dev/pcons.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: pcons.c,v 1.33 2014/03/16 05:20:26 dholland 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.33 2014/03/16 05:20:26 dholland 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 	.d_open = pconsopen,
82 	.d_close = pconsclose,
83 	.d_read = pconsread,
84 	.d_write = pconswrite,
85 	.d_ioctl = pconsioctl,
86 	.d_stop = nostop,
87 	.d_tty = pconstty,
88 	.d_poll = pconspoll,
89 	.d_mmap = nommap,
90 	.d_kqfilter = ttykqfilter,
91 	.d_flag = D_TTY
92 };
93 
94 static struct cnm_state pcons_cnm_state;
95 
96 static int pconsprobe(void);
97 extern struct consdev *cn_tab;
98 
99 static int
100 pconsmatch(device_t parent, cfdata_t match, void *aux)
101 {
102 	struct mainbus_attach_args *ma = aux;
103 	extern int  prom_cngetc(dev_t);
104 
105 	/* Only attach if no other console has attached. */
106 	return ((strcmp("pcons", ma->ma_name) == 0) &&
107 		(cn_tab->cn_getc == prom_cngetc));
108 
109 }
110 
111 static void
112 pconsattach(device_t parent, device_t self, void *aux)
113 {
114 	struct pconssoftc *sc = device_private(self);
115 	sc->of_dev = self;
116 
117 	printf("\n");
118 	if (!pconsprobe())
119 		return;
120 
121 	cn_init_magic(&pcons_cnm_state);
122 	cn_set_magic("+++++");
123 	callout_init(&sc->sc_poll_ch, 0);
124 }
125 
126 static void pconsstart(struct tty *);
127 static int pconsparam(struct tty *, struct termios *);
128 static void pcons_poll(void *);
129 
130 int
131 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
132 {
133 	struct pconssoftc *sc;
134 	struct tty *tp;
135 
136 	sc = device_lookup_private(&pcons_cd, minor(dev));
137 	if (!sc)
138 		return ENXIO;
139 	if (!(tp = sc->of_tty))
140 		sc->of_tty = tp = tty_alloc();
141 	tp->t_oproc = pconsstart;
142 	tp->t_param = pconsparam;
143 	tp->t_dev = dev;
144 	cn_tab->cn_dev = dev;
145 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
146 		return (EBUSY);
147 	if (!(tp->t_state & TS_ISOPEN)) {
148 		ttychars(tp);
149 		tp->t_iflag = TTYDEF_IFLAG;
150 		tp->t_oflag = TTYDEF_OFLAG;
151 		tp->t_cflag = TTYDEF_CFLAG;
152 		tp->t_lflag = TTYDEF_LFLAG;
153 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
154 		pconsparam(tp, &tp->t_termios);
155 		ttsetwater(tp);
156 	}
157 	tp->t_state |= TS_CARR_ON;
158 
159 	if (!(sc->of_flags & OFPOLL)) {
160 		sc->of_flags |= OFPOLL;
161 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
162 	}
163 
164 	return (*tp->t_linesw->l_open)(dev, tp);
165 }
166 
167 int
168 pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
169 {
170 	struct pconssoftc *sc = device_lookup_private(&pcons_cd,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_t dev, struct uio *uio, int flag)
182 {
183 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
184 	struct tty *tp = sc->of_tty;
185 
186 	return (*tp->t_linesw->l_read)(tp, uio, flag);
187 }
188 
189 int
190 pconswrite(dev_t dev, struct uio *uio, int flag)
191 {
192 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
193 	struct tty *tp = sc->of_tty;
194 
195 	return (*tp->t_linesw->l_write)(tp, uio, flag);
196 }
197 
198 int
199 pconspoll(dev_t dev, int events, struct lwp *l)
200 {
201 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
202 	struct tty *tp = sc->of_tty;
203 
204 	return ((*tp->t_linesw->l_poll)(tp, events, l));
205 }
206 
207 int
208 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
209 {
210 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
211 	struct tty *tp = sc->of_tty;
212 	int error;
213 
214 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
215 		return error;
216 	return ttioctl(tp, cmd, data, flag, l);
217 }
218 
219 struct tty *
220 pconstty(dev_t dev)
221 {
222 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
223 
224 	return sc->of_tty;
225 }
226 
227 static void
228 pconsstart(struct tty *tp)
229 {
230 	struct clist *cl;
231 	int s, len;
232 	u_char buf[OFBURSTLEN];
233 
234 	s = spltty();
235 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
236 		splx(s);
237 		return;
238 	}
239 	tp->t_state |= TS_BUSY;
240 	splx(s);
241 	cl = &tp->t_outq;
242 	len = q_to_b(cl, buf, OFBURSTLEN);
243 	prom_write(prom_stdout(), buf, len);
244 	s = spltty();
245 	tp->t_state &= ~TS_BUSY;
246 	if (ttypull(tp)) {
247 		tp->t_state |= TS_TIMEOUT;
248 		callout_schedule(&tp->t_rstrt_ch, 1);
249 	}
250 	splx(s);
251 }
252 
253 static int
254 pconsparam(struct tty *tp, struct termios *t)
255 {
256 	tp->t_ispeed = t->c_ispeed;
257 	tp->t_ospeed = t->c_ospeed;
258 	tp->t_cflag = t->c_cflag;
259 	return 0;
260 }
261 
262 static void
263 pcons_poll(void *aux)
264 {
265 	struct pconssoftc *sc = aux;
266 	struct tty *tp = sc->of_tty;
267 	char ch;
268 
269 	while (prom_read(prom_stdin(), &ch, 1) > 0) {
270 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
271 		if (tp && (tp->t_state & TS_ISOPEN))
272 			(*tp->t_linesw->l_rint)(ch, tp);
273 	}
274 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
275 }
276 
277 int
278 pconsprobe(void)
279 {
280 
281 	return (prom_stdin() && prom_stdout());
282 }
283 
284 void
285 pcons_cnpollc(dev_t dev, int on)
286 {
287 	struct pconssoftc *sc;
288 
289 	sc = device_lookup_private(&pcons_cd, minor(dev));
290 	if (sc == NULL)
291 		return;
292 
293 	if (on) {
294 		if (sc->of_flags & OFPOLL)
295 			callout_stop(&sc->sc_poll_ch);
296 		sc->of_flags &= ~OFPOLL;
297 	} else {
298                 /* Resuming kernel. */
299 		if (!(sc->of_flags & OFPOLL)) {
300 			sc->of_flags |= OFPOLL;
301 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
302 		}
303 	}
304 }
305 
306 void pcons_dopoll(void);
307 void
308 pcons_dopoll(void)
309 {
310 		pcons_poll(device_lookup_private(&pcons_cd, 0));
311 }
312