xref: /netbsd-src/sys/arch/sparc64/dev/pcons.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: pcons.c,v 1.29 2008/06/13 13:10:49 cegger 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.29 2008/06/13 13:10:49 cegger 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(struct device *, struct cfdata *, void *);
65 static void pconsattach(struct device *, struct device *, void *);
66 
67 CFATTACH_DECL(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 	pconsopen, pconsclose, pconsread, pconswrite, pconsioctl,
82 	nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY
83 };
84 
85 static struct cnm_state pcons_cnm_state;
86 
87 static int pconsprobe(void);
88 extern struct consdev *cn_tab;
89 
90 static int
91 pconsmatch(struct device *parent, struct cfdata *match, void *aux)
92 {
93 	struct mainbus_attach_args *ma = aux;
94 	extern int  prom_cngetc(dev_t);
95 
96 	/* Only attach if no other console has attached. */
97 	return ((strcmp("pcons", ma->ma_name) == 0) &&
98 		(cn_tab->cn_getc == prom_cngetc));
99 
100 }
101 
102 static void
103 pconsattach(struct device *parent, struct device *self, void *aux)
104 {
105 	struct pconssoftc *sc = (struct pconssoftc *) self;
106 
107 	printf("\n");
108 	if (!pconsprobe())
109 		return;
110 
111 	cn_init_magic(&pcons_cnm_state);
112 	cn_set_magic("+++++");
113 	callout_init(&sc->sc_poll_ch, 0);
114 }
115 
116 static void pconsstart(struct tty *);
117 static int pconsparam(struct tty *, struct termios *);
118 static void pcons_poll(void *);
119 
120 int
121 pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
122 {
123 	struct pconssoftc *sc;
124 	struct tty *tp;
125 	struct proc *p;
126 
127 	p = l->l_proc;
128 
129 	sc = device_lookup_private(&pcons_cd, minor(dev));
130 	if (!sc)
131 		return ENXIO;
132 	if (!(tp = sc->of_tty))
133 		sc->of_tty = tp = ttymalloc();
134 	tp->t_oproc = pconsstart;
135 	tp->t_param = pconsparam;
136 	tp->t_dev = dev;
137 	cn_tab->cn_dev = dev;
138 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
139 		return (EBUSY);
140 	if (!(tp->t_state & TS_ISOPEN)) {
141 		ttychars(tp);
142 		tp->t_iflag = TTYDEF_IFLAG;
143 		tp->t_oflag = TTYDEF_OFLAG;
144 		tp->t_cflag = TTYDEF_CFLAG;
145 		tp->t_lflag = TTYDEF_LFLAG;
146 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
147 		pconsparam(tp, &tp->t_termios);
148 		ttsetwater(tp);
149 	}
150 	tp->t_state |= TS_CARR_ON;
151 
152 	if (!(sc->of_flags & OFPOLL)) {
153 		sc->of_flags |= OFPOLL;
154 		callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
155 	}
156 
157 	return (*tp->t_linesw->l_open)(dev, tp);
158 }
159 
160 int
161 pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
162 {
163 	struct pconssoftc *sc = device_lookup_private(&pcons_cd,minor(dev));
164 	struct tty *tp = sc->of_tty;
165 
166 	callout_stop(&sc->sc_poll_ch);
167 	sc->of_flags &= ~OFPOLL;
168 	(*tp->t_linesw->l_close)(tp, flag);
169 	ttyclose(tp);
170 	return 0;
171 }
172 
173 int
174 pconsread(dev_t dev, struct uio *uio, int flag)
175 {
176 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
177 	struct tty *tp = sc->of_tty;
178 
179 	return (*tp->t_linesw->l_read)(tp, uio, flag);
180 }
181 
182 int
183 pconswrite(dev_t dev, struct uio *uio, int flag)
184 {
185 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, 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_t dev, int events, struct lwp *l)
193 {
194 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
195 	struct tty *tp = sc->of_tty;
196 
197 	return ((*tp->t_linesw->l_poll)(tp, events, l));
198 }
199 
200 int
201 pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
202 {
203 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
204 	struct tty *tp = sc->of_tty;
205 	int error;
206 
207 	if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
208 		return error;
209 	return ttioctl(tp, cmd, data, flag, l);
210 }
211 
212 struct tty *
213 pconstty(dev_t dev)
214 {
215 	struct pconssoftc *sc = device_lookup_private(&pcons_cd, 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_write(prom_stdout(), buf, len);
237 	s = spltty();
238 	tp->t_state &= ~TS_BUSY;
239 	if (ttypull(tp)) {
240 		tp->t_state |= TS_TIMEOUT;
241 		callout_schedule(&tp->t_rstrt_ch, 1);
242 	}
243 	splx(s);
244 }
245 
246 static int
247 pconsparam(struct tty *tp, struct termios *t)
248 {
249 	tp->t_ispeed = t->c_ispeed;
250 	tp->t_ospeed = t->c_ospeed;
251 	tp->t_cflag = t->c_cflag;
252 	return 0;
253 }
254 
255 static void
256 pcons_poll(void *aux)
257 {
258 	struct pconssoftc *sc = aux;
259 	struct tty *tp = sc->of_tty;
260 	char ch;
261 
262 	while (prom_read(prom_stdin(), &ch, 1) > 0) {
263 		cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
264 		if (tp && (tp->t_state & TS_ISOPEN))
265 			(*tp->t_linesw->l_rint)(ch, tp);
266 	}
267 	callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
268 }
269 
270 int
271 pconsprobe(void)
272 {
273 
274 	return (prom_stdin() && prom_stdout());
275 }
276 
277 void
278 pcons_cnpollc(dev_t dev, int on)
279 {
280 	struct pconssoftc *sc;
281 
282 	sc = device_lookup_private(&pcons_cd, minor(dev));
283 	if (sc == NULL)
284 		return;
285 
286 	if (on) {
287 		if (sc->of_flags & OFPOLL)
288 			callout_stop(&sc->sc_poll_ch);
289 		sc->of_flags &= ~OFPOLL;
290 	} else {
291                 /* Resuming kernel. */
292 		if (!(sc->of_flags & OFPOLL)) {
293 			sc->of_flags |= OFPOLL;
294 			callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
295 		}
296 	}
297 }
298 
299 void pcons_dopoll(void);
300 void
301 pcons_dopoll(void)
302 {
303 		pcons_poll(device_lookup_private(&pcons_cd, 0));
304 }
305