xref: /netbsd-src/sys/arch/sun2/dev/pcons.c (revision f9228f42259a421502f04b1ddb2df91c2ecbc519)
1 /*	$NetBSD: pcons.c,v 1.22 2014/07/25 08:10:35 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.22 2014/07/25 08:10:35 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/promlib.h>
56 #include <machine/cpu.h>
57 #include <machine/psl.h>
58 
59 #include <dev/cons.h>
60 
61 #include <sun2/dev/cons.h>
62 
63 #include "ioconf.h"
64 
65 static int pconsmatch(device_t, cfdata_t, void *);
66 static void pconsattach(device_t, device_t, void *);
67 
68 CFATTACH_DECL_NEW(pcons, sizeof(struct pconssoftc),
69     pconsmatch, pconsattach, NULL, NULL);
70 
71 static struct cnm_state pcons_cnm_state;
72 
73 static int pconsprobe(void);
74 
75 dev_type_open(pconsopen);
76 dev_type_close(pconsclose);
77 dev_type_read(pconsread);
78 dev_type_write(pconswrite);
79 dev_type_ioctl(pconsioctl);
80 dev_type_tty(pconstty);
81 dev_type_poll(pconspoll);
82 
83 const struct cdevsw pcons_cdevsw = {
84 	.d_open = pconsopen,
85 	.d_close = pconsclose,
86 	.d_read = pconsread,
87 	.d_write = pconswrite,
88 	.d_ioctl = pconsioctl,
89 	.d_stop = nostop,
90 	.d_tty = pconstty,
91 	.d_poll = pconspoll,
92 	.d_mmap = nommap,
93 	.d_kqfilter = ttykqfilter,
94 	.d_discard = nodiscard,
95 	.d_flag = D_TTY
96 };
97 
98 static int
pconsmatch(device_t parent,cfdata_t cf,void * aux)99 pconsmatch(device_t parent, cfdata_t cf, void *aux)
100 {
101 	struct mainbus_attach_args *ma = aux;
102 	extern int prom_cngetc(dev_t);
103 
104 	/* Only attach if no other console has attached. */
105 	return (ma->ma_name != NULL) &&
106 	    (strcmp("pcons", ma->ma_name) == 0) &&
107 	    (cn_tab->cn_getc == prom_cngetc);
108 
109 }
110 
111 static void
pconsattach(device_t parent,device_t self,void * aux)112 pconsattach(device_t parent, device_t self, void *aux)
113 {
114 	struct pconssoftc *sc = device_private(self);
115 
116 	aprint_normal("\n");
117 	if (!pconsprobe())
118 		return;
119 
120 	cn_init_magic(&pcons_cnm_state);
121 	cn_set_magic("+++++");
122 	callout_init(&sc->sc_poll_ch, 0);
123 }
124 
125 static void pconsstart(struct tty *);
126 static int pconsparam(struct tty *, struct termios *);
127 static void pcons_poll(void *);
128 
129 int
pconsopen(dev_t dev,int flag,int mode,struct lwp * l)130 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
131 {
132 	struct pconssoftc *sc;
133 	struct tty *tp;
134 
135 	sc = device_lookup_private(&pcons_cd, minor(dev));
136 	if (sc == NULL)
137 		return ENXIO;
138 	if ((tp = sc->of_tty) == NULL)
139 		sc->of_tty = tp = tty_alloc();
140 	tp->t_oproc = pconsstart;
141 	tp->t_param = pconsparam;
142 	tp->t_dev = dev;
143 	cn_tab->cn_dev = dev;
144 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
145 		return (EBUSY);
146 	if ((tp->t_state & TS_ISOPEN) == 0) {
147 		ttychars(tp);
148 		tp->t_iflag = TTYDEF_IFLAG;
149 		tp->t_oflag = TTYDEF_OFLAG;
150 		tp->t_cflag = TTYDEF_CFLAG;
151 		tp->t_lflag = TTYDEF_LFLAG;
152 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
153 		pconsparam(tp, &tp->t_termios);
154 		ttsetwater(tp);
155 	}
156 	tp->t_state |= TS_CARR_ON;
157 
158 	if ((sc->of_flags & OFPOLL) == 0) {
159 		sc->of_flags |= OFPOLL;
160 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
161 	}
162 
163 	return (*tp->t_linesw->l_open)(dev, tp);
164 }
165 
166 int
pconsclose(dev_t dev,int flag,int mode,struct lwp * l)167 pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
168 {
169 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
170 	struct tty *tp = sc->of_tty;
171 
172 	callout_stop(&sc->sc_poll_ch);
173 	sc->of_flags &= ~OFPOLL;
174 	(*tp->t_linesw->l_close)(tp, flag);
175 	ttyclose(tp);
176 	return 0;
177 }
178 
179 int
pconsread(dev_t dev,struct uio * uio,int flag)180 pconsread(dev_t dev, struct uio *uio, int flag)
181 {
182 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
183 	struct tty *tp = sc->of_tty;
184 
185 	return (*tp->t_linesw->l_read)(tp, uio, flag);
186 }
187 
188 int
pconswrite(dev_t dev,struct uio * uio,int flag)189 pconswrite(dev_t dev, struct uio *uio, int flag)
190 {
191 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
192 	struct tty *tp = sc->of_tty;
193 
194 	return (*tp->t_linesw->l_write)(tp, uio, flag);
195 }
196 
197 int
pconspoll(dev_t dev,int events,struct lwp * l)198 pconspoll(dev_t dev, int events, struct lwp *l)
199 {
200 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
201 	struct tty *tp = sc->of_tty;
202 
203 	return (*tp->t_linesw->l_poll)(tp, events, l);
204 }
205 
206 int
pconsioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)207 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
208 {
209 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
210 	struct tty *tp = sc->of_tty;
211 	int error;
212 
213 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) >= 0)
214 		return error;
215 	if ((error = ttioctl(tp, cmd, data, flag, l)) >= 0)
216 		return error;
217 	return ENOTTY;
218 }
219 
220 struct tty *
pconstty(dev_t dev)221 pconstty(dev_t dev)
222 {
223 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
224 
225 	return sc->of_tty;
226 }
227 
228 static void
pconsstart(struct tty * tp)229 pconsstart(struct tty *tp)
230 {
231 	struct clist *cl;
232 	int s, len;
233 	uint8_t buf[OFBURSTLEN];
234 
235 	s = spltty();
236 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
237 		splx(s);
238 		return;
239 	}
240 	tp->t_state |= TS_BUSY;
241 	splx(s);
242 	cl = &tp->t_outq;
243 	len = q_to_b(cl, buf, OFBURSTLEN);
244 	prom_putstr(buf, len);
245 	s = spltty();
246 	tp->t_state &= ~TS_BUSY;
247 	if (ttypull(tp)) {
248 		tp->t_state |= TS_TIMEOUT;
249 		callout_schedule(&tp->t_rstrt_ch, 1);
250 	}
251 	splx(s);
252 }
253 
254 static int
pconsparam(struct tty * tp,struct termios * t)255 pconsparam(struct tty *tp, struct termios *t)
256 {
257 
258 	tp->t_ispeed = t->c_ispeed;
259 	tp->t_ospeed = t->c_ospeed;
260 	tp->t_cflag = t->c_cflag;
261 	return 0;
262 }
263 
264 static void
pcons_poll(void * aux)265 pcons_poll(void *aux)
266 {
267 	struct pconssoftc *sc = aux;
268 	struct tty *tp = sc->of_tty;
269 	int c;
270 	char ch;
271 
272 	while ((c = prom_peekchar()) >= 0) {
273 		ch = c;
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
pconsprobe(void)282 pconsprobe(void)
283 {
284 
285 	switch (prom_version()) {
286 #ifdef	PROM_OLDMON
287 	case PROM_OLDMON:
288 	case PROM_OBP_V0:
289 		return 1;
290 #endif	/* PROM_OLDMON */
291 #ifdef	PROM_OBP_V2
292 	case PROM_OBP_V2:
293 	case PROM_OBP_V3:
294 	case PROM_OPENFIRM:
295 		return prom_stdin() && prom_stdout();
296 #endif	/* PROM_OBP_V2 */
297 	default:
298 		break;
299 	}
300 	return 0;
301 }
302 
303 void
pcons_cnpollc(dev_t dev,int on)304 pcons_cnpollc(dev_t dev, int on)
305 {
306 	struct pconssoftc *sc;
307 
308 	sc = device_lookup_private(&pcons_cd, minor(dev));
309 	if (sc == NULL)
310 		return;
311 
312 	if (on) {
313 		if (sc->of_flags & OFPOLL)
314 			callout_stop(&sc->sc_poll_ch);
315 		sc->of_flags &= ~OFPOLL;
316 	} else {
317                 /* Resuming kernel. */
318 		if ((sc->of_flags & OFPOLL) == 0) {
319 			sc->of_flags |= OFPOLL;
320 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
321 		}
322 	}
323 }
324 
325 void pcons_dopoll(void);
326 
327 void
pcons_dopoll(void)328 pcons_dopoll(void)
329 {
330 
331 	pcons_poll(device_lookup_private(&pcons_cd, 0));
332 }
333