pcons.c revision 1.6.2.3 1 1.6.2.3 bouyer /* $NetBSD: pcons.c,v 1.6.2.3 2000/11/22 16:01:47 bouyer Exp $ */
2 1.6.2.2 bouyer
3 1.6.2.2 bouyer /*-
4 1.6.2.2 bouyer * Copyright (c) 2000 Eduardo E. Horvath
5 1.6.2.2 bouyer * All rights reserved.
6 1.6.2.2 bouyer *
7 1.6.2.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.6.2.2 bouyer * modification, are permitted provided that the following conditions
9 1.6.2.2 bouyer * are met:
10 1.6.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.6.2.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.6.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.6.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.6.2.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.6.2.2 bouyer * 3. The name of the author may not be used to endorse or promote products
16 1.6.2.2 bouyer * derived from this software without specific prior written permission.
17 1.6.2.2 bouyer *
18 1.6.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.6.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.6.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.6.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.6.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.6.2.2 bouyer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.6.2.2 bouyer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.6.2.2 bouyer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.6.2.2 bouyer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.6.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.6.2.2 bouyer * SUCH DAMAGE.
29 1.6.2.2 bouyer */
30 1.6.2.2 bouyer
31 1.6.2.2 bouyer /*
32 1.6.2.2 bouyer * Default console driver. Uses the PROM or whatever
33 1.6.2.2 bouyer * driver(s) are appropriate.
34 1.6.2.2 bouyer */
35 1.6.2.2 bouyer
36 1.6.2.2 bouyer #include "opt_ddb.h"
37 1.6.2.2 bouyer
38 1.6.2.2 bouyer #include <sys/param.h>
39 1.6.2.2 bouyer #include <sys/systm.h>
40 1.6.2.2 bouyer #include <sys/conf.h>
41 1.6.2.2 bouyer #include <sys/device.h>
42 1.6.2.2 bouyer #include <sys/file.h>
43 1.6.2.2 bouyer #include <sys/ioctl.h>
44 1.6.2.2 bouyer #include <sys/kernel.h>
45 1.6.2.2 bouyer #include <sys/proc.h>
46 1.6.2.2 bouyer #include <sys/tty.h>
47 1.6.2.2 bouyer #include <sys/time.h>
48 1.6.2.2 bouyer #include <sys/syslog.h>
49 1.6.2.2 bouyer
50 1.6.2.2 bouyer #include <machine/autoconf.h>
51 1.6.2.2 bouyer #include <machine/openfirm.h>
52 1.6.2.2 bouyer #include <machine/bsd_openprom.h>
53 1.6.2.2 bouyer #include <machine/conf.h>
54 1.6.2.2 bouyer #include <machine/cpu.h>
55 1.6.2.2 bouyer #include <machine/eeprom.h>
56 1.6.2.2 bouyer #include <machine/psl.h>
57 1.6.2.2 bouyer
58 1.6.2.2 bouyer #include <dev/cons.h>
59 1.6.2.2 bouyer
60 1.6.2.2 bouyer #include <sparc64/dev/cons.h>
61 1.6.2.2 bouyer
62 1.6.2.2 bouyer static int pconsmatch __P((struct device *, struct cfdata *, void *));
63 1.6.2.2 bouyer static void pconsattach __P((struct device *, struct device *, void *));
64 1.6.2.2 bouyer
65 1.6.2.2 bouyer struct cfattach pcons_ca = {
66 1.6.2.2 bouyer sizeof(struct pconssoftc), pconsmatch, pconsattach
67 1.6.2.2 bouyer };
68 1.6.2.2 bouyer
69 1.6.2.2 bouyer extern struct cfdriver pcons_cd;
70 1.6.2.3 bouyer static struct cnm_state pcons_cnm_state;
71 1.6.2.2 bouyer
72 1.6.2.2 bouyer static int pconsprobe __P((void));
73 1.6.2.3 bouyer extern struct consdev *cn_tab;
74 1.6.2.2 bouyer
75 1.6.2.2 bouyer static int
76 1.6.2.2 bouyer pconsmatch(parent, match, aux)
77 1.6.2.2 bouyer struct device *parent;
78 1.6.2.2 bouyer struct cfdata *match;
79 1.6.2.2 bouyer void *aux;
80 1.6.2.2 bouyer {
81 1.6.2.2 bouyer struct mainbus_attach_args *ma = aux;
82 1.6.2.2 bouyer extern int prom_cngetc __P((dev_t));
83 1.6.2.2 bouyer
84 1.6.2.2 bouyer /* Only attach if no other console has attached. */
85 1.6.2.2 bouyer return ((strcmp("pcons", ma->ma_name) == 0) &&
86 1.6.2.2 bouyer (cn_tab->cn_getc == prom_cngetc));
87 1.6.2.2 bouyer
88 1.6.2.2 bouyer }
89 1.6.2.2 bouyer
90 1.6.2.2 bouyer static void
91 1.6.2.2 bouyer pconsattach(parent, self, aux)
92 1.6.2.2 bouyer struct device *parent, *self;
93 1.6.2.2 bouyer void *aux;
94 1.6.2.2 bouyer {
95 1.6.2.2 bouyer struct pconssoftc *sc = (struct pconssoftc *) self;
96 1.6.2.2 bouyer
97 1.6.2.2 bouyer printf("\n");
98 1.6.2.2 bouyer if (!pconsprobe())
99 1.6.2.2 bouyer return;
100 1.6.2.2 bouyer
101 1.6.2.3 bouyer cn_init_magic(&pcons_cnm_state);
102 1.6.2.3 bouyer cn_set_magic("+++++");
103 1.6.2.2 bouyer callout_init(&sc->sc_poll_ch);
104 1.6.2.2 bouyer }
105 1.6.2.2 bouyer
106 1.6.2.2 bouyer static void pconsstart __P((struct tty *));
107 1.6.2.2 bouyer static int pconsparam __P((struct tty *, struct termios *));
108 1.6.2.2 bouyer static void pcons_poll __P((void *));
109 1.6.2.2 bouyer
110 1.6.2.2 bouyer int
111 1.6.2.2 bouyer pconsopen(dev, flag, mode, p)
112 1.6.2.2 bouyer dev_t dev;
113 1.6.2.2 bouyer int flag, mode;
114 1.6.2.2 bouyer struct proc *p;
115 1.6.2.2 bouyer {
116 1.6.2.2 bouyer struct pconssoftc *sc;
117 1.6.2.2 bouyer int unit = minor(dev);
118 1.6.2.2 bouyer struct tty *tp;
119 1.6.2.2 bouyer
120 1.6.2.2 bouyer if (unit >= pcons_cd.cd_ndevs)
121 1.6.2.2 bouyer return ENXIO;
122 1.6.2.2 bouyer sc = pcons_cd.cd_devs[unit];
123 1.6.2.2 bouyer if (!sc)
124 1.6.2.2 bouyer return ENXIO;
125 1.6.2.2 bouyer if (!(tp = sc->of_tty))
126 1.6.2.2 bouyer sc->of_tty = tp = ttymalloc();
127 1.6.2.2 bouyer tp->t_oproc = pconsstart;
128 1.6.2.2 bouyer tp->t_param = pconsparam;
129 1.6.2.2 bouyer tp->t_dev = dev;
130 1.6.2.3 bouyer cn_tab->cn_dev = dev;
131 1.6.2.2 bouyer if (!(tp->t_state & TS_ISOPEN)) {
132 1.6.2.2 bouyer ttychars(tp);
133 1.6.2.2 bouyer tp->t_iflag = TTYDEF_IFLAG;
134 1.6.2.2 bouyer tp->t_oflag = TTYDEF_OFLAG;
135 1.6.2.2 bouyer tp->t_cflag = TTYDEF_CFLAG;
136 1.6.2.2 bouyer tp->t_lflag = TTYDEF_LFLAG;
137 1.6.2.2 bouyer tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
138 1.6.2.2 bouyer pconsparam(tp, &tp->t_termios);
139 1.6.2.2 bouyer ttsetwater(tp);
140 1.6.2.2 bouyer } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
141 1.6.2.2 bouyer return EBUSY;
142 1.6.2.2 bouyer tp->t_state |= TS_CARR_ON;
143 1.6.2.2 bouyer
144 1.6.2.2 bouyer if (!(sc->of_flags & OFPOLL)) {
145 1.6.2.2 bouyer sc->of_flags |= OFPOLL;
146 1.6.2.2 bouyer callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
147 1.6.2.2 bouyer }
148 1.6.2.2 bouyer
149 1.6.2.3 bouyer return (*tp->t_linesw->l_open)(dev, tp);
150 1.6.2.2 bouyer }
151 1.6.2.2 bouyer
152 1.6.2.2 bouyer int
153 1.6.2.2 bouyer pconsclose(dev, flag, mode, p)
154 1.6.2.2 bouyer dev_t dev;
155 1.6.2.2 bouyer int flag, mode;
156 1.6.2.2 bouyer struct proc *p;
157 1.6.2.2 bouyer {
158 1.6.2.2 bouyer struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
159 1.6.2.2 bouyer struct tty *tp = sc->of_tty;
160 1.6.2.2 bouyer
161 1.6.2.2 bouyer callout_stop(&sc->sc_poll_ch);
162 1.6.2.2 bouyer sc->of_flags &= ~OFPOLL;
163 1.6.2.3 bouyer (*tp->t_linesw->l_close)(tp, flag);
164 1.6.2.2 bouyer ttyclose(tp);
165 1.6.2.2 bouyer return 0;
166 1.6.2.2 bouyer }
167 1.6.2.2 bouyer
168 1.6.2.2 bouyer int
169 1.6.2.2 bouyer pconsread(dev, uio, flag)
170 1.6.2.2 bouyer dev_t dev;
171 1.6.2.2 bouyer struct uio *uio;
172 1.6.2.2 bouyer int flag;
173 1.6.2.2 bouyer {
174 1.6.2.2 bouyer struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
175 1.6.2.2 bouyer struct tty *tp = sc->of_tty;
176 1.6.2.2 bouyer
177 1.6.2.3 bouyer return (*tp->t_linesw->l_read)(tp, uio, flag);
178 1.6.2.2 bouyer }
179 1.6.2.2 bouyer
180 1.6.2.2 bouyer int
181 1.6.2.2 bouyer pconswrite(dev, uio, flag)
182 1.6.2.2 bouyer dev_t dev;
183 1.6.2.2 bouyer struct uio *uio;
184 1.6.2.2 bouyer int flag;
185 1.6.2.2 bouyer {
186 1.6.2.2 bouyer struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
187 1.6.2.2 bouyer struct tty *tp = sc->of_tty;
188 1.6.2.2 bouyer
189 1.6.2.3 bouyer return (*tp->t_linesw->l_write)(tp, uio, flag);
190 1.6.2.2 bouyer }
191 1.6.2.2 bouyer
192 1.6.2.2 bouyer int
193 1.6.2.2 bouyer pconsioctl(dev, cmd, data, flag, p)
194 1.6.2.2 bouyer dev_t dev;
195 1.6.2.2 bouyer u_long cmd;
196 1.6.2.2 bouyer caddr_t data;
197 1.6.2.2 bouyer int flag;
198 1.6.2.2 bouyer struct proc *p;
199 1.6.2.2 bouyer {
200 1.6.2.2 bouyer struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
201 1.6.2.2 bouyer struct tty *tp = sc->of_tty;
202 1.6.2.2 bouyer int error;
203 1.6.2.2 bouyer
204 1.6.2.3 bouyer if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
205 1.6.2.2 bouyer return error;
206 1.6.2.2 bouyer if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
207 1.6.2.2 bouyer return error;
208 1.6.2.2 bouyer return ENOTTY;
209 1.6.2.2 bouyer }
210 1.6.2.2 bouyer
211 1.6.2.2 bouyer struct tty *
212 1.6.2.2 bouyer pconstty(dev)
213 1.6.2.2 bouyer dev_t dev;
214 1.6.2.2 bouyer {
215 1.6.2.2 bouyer struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
216 1.6.2.2 bouyer
217 1.6.2.2 bouyer return sc->of_tty;
218 1.6.2.2 bouyer }
219 1.6.2.2 bouyer
220 1.6.2.2 bouyer void
221 1.6.2.2 bouyer pconsstop(tp, flag)
222 1.6.2.2 bouyer struct tty *tp;
223 1.6.2.2 bouyer int flag;
224 1.6.2.2 bouyer {
225 1.6.2.2 bouyer }
226 1.6.2.2 bouyer
227 1.6.2.2 bouyer static void
228 1.6.2.2 bouyer pconsstart(tp)
229 1.6.2.2 bouyer struct tty *tp;
230 1.6.2.2 bouyer {
231 1.6.2.2 bouyer struct clist *cl;
232 1.6.2.2 bouyer int s, len;
233 1.6.2.2 bouyer u_char buf[OFBURSTLEN];
234 1.6.2.2 bouyer
235 1.6.2.2 bouyer s = spltty();
236 1.6.2.2 bouyer if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
237 1.6.2.2 bouyer splx(s);
238 1.6.2.2 bouyer return;
239 1.6.2.2 bouyer }
240 1.6.2.2 bouyer tp->t_state |= TS_BUSY;
241 1.6.2.2 bouyer splx(s);
242 1.6.2.2 bouyer cl = &tp->t_outq;
243 1.6.2.2 bouyer len = q_to_b(cl, buf, OFBURSTLEN);
244 1.6.2.2 bouyer OF_write(stdout, buf, len);
245 1.6.2.2 bouyer s = spltty();
246 1.6.2.2 bouyer tp->t_state &= ~TS_BUSY;
247 1.6.2.2 bouyer if (cl->c_cc) {
248 1.6.2.2 bouyer tp->t_state |= TS_TIMEOUT;
249 1.6.2.2 bouyer callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
250 1.6.2.2 bouyer }
251 1.6.2.2 bouyer if (cl->c_cc <= tp->t_lowat) {
252 1.6.2.2 bouyer if (tp->t_state & TS_ASLEEP) {
253 1.6.2.2 bouyer tp->t_state &= ~TS_ASLEEP;
254 1.6.2.2 bouyer wakeup(cl);
255 1.6.2.2 bouyer }
256 1.6.2.2 bouyer selwakeup(&tp->t_wsel);
257 1.6.2.2 bouyer }
258 1.6.2.2 bouyer splx(s);
259 1.6.2.2 bouyer }
260 1.6.2.2 bouyer
261 1.6.2.2 bouyer static int
262 1.6.2.2 bouyer pconsparam(tp, t)
263 1.6.2.2 bouyer struct tty *tp;
264 1.6.2.2 bouyer struct termios *t;
265 1.6.2.2 bouyer {
266 1.6.2.2 bouyer tp->t_ispeed = t->c_ispeed;
267 1.6.2.2 bouyer tp->t_ospeed = t->c_ospeed;
268 1.6.2.2 bouyer tp->t_cflag = t->c_cflag;
269 1.6.2.2 bouyer return 0;
270 1.6.2.2 bouyer }
271 1.6.2.2 bouyer
272 1.6.2.2 bouyer static void
273 1.6.2.2 bouyer pcons_poll(aux)
274 1.6.2.2 bouyer void *aux;
275 1.6.2.2 bouyer {
276 1.6.2.2 bouyer struct pconssoftc *sc = aux;
277 1.6.2.2 bouyer struct tty *tp = sc->of_tty;
278 1.6.2.2 bouyer char ch;
279 1.6.2.2 bouyer
280 1.6.2.2 bouyer while (OF_read(stdin, &ch, 1) > 0) {
281 1.6.2.3 bouyer cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
282 1.6.2.2 bouyer if (tp && (tp->t_state & TS_ISOPEN))
283 1.6.2.3 bouyer (*tp->t_linesw->l_rint)(ch, tp);
284 1.6.2.2 bouyer }
285 1.6.2.2 bouyer callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
286 1.6.2.2 bouyer }
287 1.6.2.2 bouyer
288 1.6.2.2 bouyer int
289 1.6.2.2 bouyer pconsprobe()
290 1.6.2.2 bouyer {
291 1.6.2.2 bouyer if (!stdin) stdin = OF_stdin();
292 1.6.2.2 bouyer if (!stdout) stdout = OF_stdout();
293 1.6.2.2 bouyer
294 1.6.2.2 bouyer return (stdin && stdout);
295 1.6.2.2 bouyer }
296 1.6.2.2 bouyer
297 1.6.2.2 bouyer void
298 1.6.2.2 bouyer pcons_cnpollc(dev, on)
299 1.6.2.2 bouyer dev_t dev;
300 1.6.2.2 bouyer int on;
301 1.6.2.2 bouyer {
302 1.6.2.2 bouyer struct pconssoftc *sc = NULL;
303 1.6.2.2 bouyer
304 1.6.2.2 bouyer if (pcons_cd.cd_devs)
305 1.6.2.2 bouyer sc = pcons_cd.cd_devs[minor(dev)];
306 1.6.2.2 bouyer
307 1.6.2.2 bouyer if (on) {
308 1.6.2.2 bouyer if (!sc) return;
309 1.6.2.2 bouyer if (sc->of_flags & OFPOLL)
310 1.6.2.2 bouyer callout_stop(&sc->sc_poll_ch);
311 1.6.2.2 bouyer sc->of_flags &= ~OFPOLL;
312 1.6.2.2 bouyer } else {
313 1.6.2.2 bouyer /* Resuming kernel. */
314 1.6.2.2 bouyer if (sc && !(sc->of_flags & OFPOLL)) {
315 1.6.2.2 bouyer sc->of_flags |= OFPOLL;
316 1.6.2.2 bouyer callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
317 1.6.2.2 bouyer }
318 1.6.2.2 bouyer }
319 1.6.2.2 bouyer }
320 1.6.2.2 bouyer
321 1.6.2.2 bouyer void pcons_dopoll __P((void));
322 1.6.2.2 bouyer void
323 1.6.2.2 bouyer pcons_dopoll() {
324 1.6.2.2 bouyer pcons_poll((void*)pcons_cd.cd_devs[0]);
325 1.6.2.2 bouyer }
326