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