xref: /netbsd-src/sys/arch/sun2/dev/pcons.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: pcons.c,v 1.9 2005/12/11 12:19:16 christos 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.9 2005/12/11 12:19:16 christos 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 
53 #include <machine/autoconf.h>
54 #include <machine/promlib.h>
55 #include <machine/cpu.h>
56 #include <machine/psl.h>
57 
58 #include <dev/cons.h>
59 
60 #include <sun2/dev/cons.h>
61 
62 static int pconsmatch(struct device *, struct cfdata *, void *);
63 static void pconsattach(struct device *, struct device *, void *);
64 
65 CFATTACH_DECL(pcons, sizeof(struct pconssoftc),
66     pconsmatch, pconsattach, NULL, NULL);
67 
68 extern struct cfdriver pcons_cd;
69 static struct cnm_state pcons_cnm_state;
70 
71 static int pconsprobe(void);
72 extern struct consdev *cn_tab;
73 
74 dev_type_open(pconsopen);
75 dev_type_close(pconsclose);
76 dev_type_read(pconsread);
77 dev_type_write(pconswrite);
78 dev_type_ioctl(pconsioctl);
79 dev_type_tty(pconstty);
80 dev_type_poll(pconspoll);
81 
82 const struct cdevsw pcons_cdevsw = {
83 	pconsopen, pconsclose, pconsread, pconswrite, pconsioctl,
84 	nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY
85 };
86 
87 static int
88 pconsmatch(struct device *parent, struct cfdata *match, void *aux)
89 {
90 	struct mainbus_attach_args *ma = aux;
91 	extern int  prom_cngetc(dev_t);
92 
93 	/* Only attach if no other console has attached. */
94 	return ((ma->ma_name != NULL) &&
95 		(strcmp("pcons", ma->ma_name) == 0) &&
96 		(cn_tab->cn_getc == prom_cngetc));
97 
98 }
99 
100 static void
101 pconsattach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct pconssoftc *sc = (struct pconssoftc *) self;
104 
105 	printf("\n");
106 	if (!pconsprobe())
107 		return;
108 
109 	cn_init_magic(&pcons_cnm_state);
110 	cn_set_magic("+++++");
111 	callout_init(&sc->sc_poll_ch);
112 }
113 
114 static void pconsstart(struct tty *);
115 static int pconsparam(struct tty *, struct termios *);
116 static void pcons_poll(void *);
117 
118 int
119 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
120 {
121 	struct pconssoftc *sc;
122 	int unit = minor(dev);
123 	struct tty *tp;
124 
125 	if (unit >= pcons_cd.cd_ndevs)
126 		return ENXIO;
127 	sc = pcons_cd.cd_devs[unit];
128 	if (!sc)
129 		return ENXIO;
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_dev = dev;
135 	cn_tab->cn_dev = dev;
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) &&
146 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
147 		return EBUSY;
148 	tp->t_state |= TS_CARR_ON;
149 
150 	if (!(sc->of_flags & OFPOLL)) {
151 		sc->of_flags |= OFPOLL;
152 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
153 	}
154 
155 	return (*tp->t_linesw->l_open)(dev, tp);
156 }
157 
158 int
159 pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
160 {
161 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
162 	struct tty *tp = sc->of_tty;
163 
164 	callout_stop(&sc->sc_poll_ch);
165 	sc->of_flags &= ~OFPOLL;
166 	(*tp->t_linesw->l_close)(tp, flag);
167 	ttyclose(tp);
168 	return 0;
169 }
170 
171 int
172 pconsread(dev_t dev, struct uio *uio, int flag)
173 {
174 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
175 	struct tty *tp = sc->of_tty;
176 
177 	return (*tp->t_linesw->l_read)(tp, uio, flag);
178 }
179 
180 int
181 pconswrite(dev_t dev, struct uio *uio, int flag)
182 {
183 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
184 	struct tty *tp = sc->of_tty;
185 
186 	return (*tp->t_linesw->l_write)(tp, uio, flag);
187 }
188 
189 int
190 pconspoll(dev_t dev, int events, struct lwp *l)
191 {
192 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
193 	struct tty *tp = sc->of_tty;
194 
195 	return ((*tp->t_linesw->l_poll)(tp, events, l));
196 }
197 
198 int
199 pconsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
200 {
201 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
202 	struct tty *tp = sc->of_tty;
203 	int error;
204 
205 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) >= 0)
206 		return error;
207 	if ((error = ttioctl(tp, cmd, data, flag, l)) >= 0)
208 		return error;
209 	return ENOTTY;
210 }
211 
212 struct tty *
213 pconstty(dev_t dev)
214 {
215 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
216 
217 	return sc->of_tty;
218 }
219 
220 static void
221 pconsstart(struct tty *tp)
222 {
223 	struct clist *cl;
224 	int s, len;
225 	u_char buf[OFBURSTLEN];
226 
227 	s = spltty();
228 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
229 		splx(s);
230 		return;
231 	}
232 	tp->t_state |= TS_BUSY;
233 	splx(s);
234 	cl = &tp->t_outq;
235 	len = q_to_b(cl, buf, OFBURSTLEN);
236 	prom_putstr(buf, len);
237 	s = spltty();
238 	tp->t_state &= ~TS_BUSY;
239 	if (cl->c_cc) {
240 		tp->t_state |= TS_TIMEOUT;
241 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
242 	}
243 	if (cl->c_cc <= tp->t_lowat) {
244 		if (tp->t_state & TS_ASLEEP) {
245 			tp->t_state &= ~TS_ASLEEP;
246 			wakeup(cl);
247 		}
248 		selwakeup(&tp->t_wsel);
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 	int c;
268 	char ch;
269 
270 	while ((c = prom_peekchar()) >= 0) {
271 		ch = c;
272 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
273 		if (tp && (tp->t_state & TS_ISOPEN))
274 			(*tp->t_linesw->l_rint)(ch, tp);
275 	}
276 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
277 }
278 
279 int
280 pconsprobe(void)
281 {
282 	switch (prom_version()) {
283 #ifdef	PROM_OLDMON
284 	case PROM_OLDMON:
285 	case PROM_OBP_V0:
286 		return (1);
287 #endif	/* PROM_OLDMON */
288 #ifdef	PROM_OBP_V2
289 	case PROM_OBP_V2:
290 	case PROM_OBP_V3:
291 	case PROM_OPENFIRM:
292 		return (prom_stdin() && prom_stdout());
293 #endif	/* PROM_OBP_V2 */
294 	default: break;
295 	}
296 	return (0);
297 }
298 
299 void
300 pcons_cnpollc(dev_t dev, int on)
301 {
302 	struct pconssoftc *sc = NULL;
303 
304 	if (pcons_cd.cd_devs)
305 		sc = pcons_cd.cd_devs[minor(dev)];
306 
307 	if (on) {
308 		if (!sc) return;
309 		if (sc->of_flags & OFPOLL)
310 			callout_stop(&sc->sc_poll_ch);
311 		sc->of_flags &= ~OFPOLL;
312 	} else {
313                 /* Resuming kernel. */
314 		if (sc && !(sc->of_flags & OFPOLL)) {
315 			sc->of_flags |= OFPOLL;
316 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
317 		}
318 	}
319 }
320 
321 void pcons_dopoll(void);
322 void
323 pcons_dopoll(void)
324 {
325 	pcons_poll((void*)pcons_cd.cd_devs[0]);
326 }
327