xref: /netbsd/sys/arch/sparc64/dev/pcons.c (revision bf9ec67e)
1 /*	$NetBSD: pcons.c,v 1.9 2002/03/17 19:40:51 atatat 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 "opt_ddb.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49 
50 #include <machine/autoconf.h>
51 #include <machine/openfirm.h>
52 #include <machine/conf.h>
53 #include <machine/cpu.h>
54 #include <machine/eeprom.h>
55 #include <machine/psl.h>
56 
57 #include <dev/cons.h>
58 
59 #include <sparc64/dev/cons.h>
60 
61 static int pconsmatch __P((struct device *, struct cfdata *, void *));
62 static void pconsattach __P((struct device *, struct device *, void *));
63 
64 struct cfattach pcons_ca = {
65 	sizeof(struct pconssoftc), pconsmatch, pconsattach
66 };
67 
68 extern struct cfdriver pcons_cd;
69 static struct cnm_state pcons_cnm_state;
70 
71 static int pconsprobe __P((void));
72 extern struct consdev *cn_tab;
73 
74 static int
75 pconsmatch(parent, match, aux)
76 	struct device *parent;
77 	struct cfdata *match;
78 	void *aux;
79 {
80 	struct mainbus_attach_args *ma = aux;
81 	extern int  prom_cngetc __P((dev_t));
82 
83 	/* Only attach if no other console has attached. */
84 	return ((strcmp("pcons", ma->ma_name) == 0) &&
85 		(cn_tab->cn_getc == prom_cngetc));
86 
87 }
88 
89 static void
90 pconsattach(parent, self, aux)
91 	struct device *parent, *self;
92 	void *aux;
93 {
94 	struct pconssoftc *sc = (struct pconssoftc *) self;
95 
96 	printf("\n");
97 	if (!pconsprobe())
98 		return;
99 
100 	cn_init_magic(&pcons_cnm_state);
101 	cn_set_magic("+++++");
102 	callout_init(&sc->sc_poll_ch);
103 }
104 
105 static void pconsstart __P((struct tty *));
106 static int pconsparam __P((struct tty *, struct termios *));
107 static void pcons_poll __P((void *));
108 
109 int
110 pconsopen(dev, flag, mode, p)
111 	dev_t dev;
112 	int flag, mode;
113 	struct proc *p;
114 {
115 	struct pconssoftc *sc;
116 	int unit = minor(dev);
117 	struct tty *tp;
118 
119 	if (unit >= pcons_cd.cd_ndevs)
120 		return ENXIO;
121 	sc = pcons_cd.cd_devs[unit];
122 	if (!sc)
123 		return ENXIO;
124 	if (!(tp = sc->of_tty))
125 		sc->of_tty = tp = ttymalloc();
126 	tp->t_oproc = pconsstart;
127 	tp->t_param = pconsparam;
128 	tp->t_dev = dev;
129 	cn_tab->cn_dev = dev;
130 	if (!(tp->t_state & TS_ISOPEN)) {
131 		ttychars(tp);
132 		tp->t_iflag = TTYDEF_IFLAG;
133 		tp->t_oflag = TTYDEF_OFLAG;
134 		tp->t_cflag = TTYDEF_CFLAG;
135 		tp->t_lflag = TTYDEF_LFLAG;
136 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
137 		pconsparam(tp, &tp->t_termios);
138 		ttsetwater(tp);
139 	} else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
140 		return EBUSY;
141 	tp->t_state |= TS_CARR_ON;
142 
143 	if (!(sc->of_flags & OFPOLL)) {
144 		sc->of_flags |= OFPOLL;
145 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
146 	}
147 
148 	return (*tp->t_linesw->l_open)(dev, tp);
149 }
150 
151 int
152 pconsclose(dev, flag, mode, p)
153 	dev_t dev;
154 	int flag, mode;
155 	struct proc *p;
156 {
157 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
158 	struct tty *tp = sc->of_tty;
159 
160 	callout_stop(&sc->sc_poll_ch);
161 	sc->of_flags &= ~OFPOLL;
162 	(*tp->t_linesw->l_close)(tp, flag);
163 	ttyclose(tp);
164 	return 0;
165 }
166 
167 int
168 pconsread(dev, uio, flag)
169 	dev_t dev;
170 	struct uio *uio;
171 	int flag;
172 {
173 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
174 	struct tty *tp = sc->of_tty;
175 
176 	return (*tp->t_linesw->l_read)(tp, uio, flag);
177 }
178 
179 int
180 pconswrite(dev, uio, flag)
181 	dev_t dev;
182 	struct uio *uio;
183 	int flag;
184 {
185 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
186 	struct tty *tp = sc->of_tty;
187 
188 	return (*tp->t_linesw->l_write)(tp, uio, flag);
189 }
190 
191 int
192 pconspoll(dev, events, p)
193 	dev_t dev;
194 	int events;
195 	struct proc *p;
196 {
197 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
198 	struct tty *tp = sc->of_tty;
199 
200 	return ((*tp->t_linesw->l_poll)(tp, events, p));
201 }
202 
203 int
204 pconsioctl(dev, cmd, data, flag, p)
205 	dev_t dev;
206 	u_long cmd;
207 	caddr_t data;
208 	int flag;
209 	struct proc *p;
210 {
211 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
212 	struct tty *tp = sc->of_tty;
213 	int error;
214 
215 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) != EPASSTHROUGH)
216 		return error;
217 	return ttioctl(tp, cmd, data, flag, p);
218 }
219 
220 struct tty *
221 pconstty(dev)
222 	dev_t dev;
223 {
224 	struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
225 
226 	return sc->of_tty;
227 }
228 
229 void
230 pconsstop(tp, flag)
231 	struct tty *tp;
232 	int flag;
233 {
234 }
235 
236 static void
237 pconsstart(tp)
238 	struct tty *tp;
239 {
240 	struct clist *cl;
241 	int s, len;
242 	u_char buf[OFBURSTLEN];
243 
244 	s = spltty();
245 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
246 		splx(s);
247 		return;
248 	}
249 	tp->t_state |= TS_BUSY;
250 	splx(s);
251 	cl = &tp->t_outq;
252 	len = q_to_b(cl, buf, OFBURSTLEN);
253 	OF_write(stdout, buf, len);
254 	s = spltty();
255 	tp->t_state &= ~TS_BUSY;
256 	if (cl->c_cc) {
257 		tp->t_state |= TS_TIMEOUT;
258 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
259 	}
260 	if (cl->c_cc <= tp->t_lowat) {
261 		if (tp->t_state & TS_ASLEEP) {
262 			tp->t_state &= ~TS_ASLEEP;
263 			wakeup(cl);
264 		}
265 		selwakeup(&tp->t_wsel);
266 	}
267 	splx(s);
268 }
269 
270 static int
271 pconsparam(tp, t)
272 	struct tty *tp;
273 	struct termios *t;
274 {
275 	tp->t_ispeed = t->c_ispeed;
276 	tp->t_ospeed = t->c_ospeed;
277 	tp->t_cflag = t->c_cflag;
278 	return 0;
279 }
280 
281 static void
282 pcons_poll(aux)
283 	void *aux;
284 {
285 	struct pconssoftc *sc = aux;
286 	struct tty *tp = sc->of_tty;
287 	char ch;
288 
289 	while (OF_read(stdin, &ch, 1) > 0) {
290 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
291 		if (tp && (tp->t_state & TS_ISOPEN))
292 			(*tp->t_linesw->l_rint)(ch, tp);
293 	}
294 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
295 }
296 
297 int
298 pconsprobe()
299 {
300 	if (!stdin) stdin = OF_stdin();
301 	if (!stdout) stdout = OF_stdout();
302 
303 	return (stdin && stdout);
304 }
305 
306 void
307 pcons_cnpollc(dev, on)
308 	dev_t dev;
309 	int on;
310 {
311 	struct pconssoftc *sc = NULL;
312 
313 	if (pcons_cd.cd_devs)
314 		sc = pcons_cd.cd_devs[minor(dev)];
315 
316 	if (on) {
317 		if (!sc) return;
318 		if (sc->of_flags & OFPOLL)
319 			callout_stop(&sc->sc_poll_ch);
320 		sc->of_flags &= ~OFPOLL;
321 	} else {
322                 /* Resuming kernel. */
323 		if (sc && !(sc->of_flags & OFPOLL)) {
324 			sc->of_flags |= OFPOLL;
325 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
326 		}
327 	}
328 }
329 
330 void pcons_dopoll __P((void));
331 void
332 pcons_dopoll() {
333 		pcons_poll((void*)pcons_cd.cd_devs[0]);
334 }
335