pcons.c revision 1.1.6.1 1 /* $NetBSD: pcons.c,v 1.1.6.1 2002/05/17 15:40:50 gehenna 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/promlib.h>
52 #include <machine/cpu.h>
53 #include <machine/psl.h>
54
55 #include <dev/cons.h>
56
57 #include <sun2/dev/cons.h>
58
59 static int pconsmatch __P((struct device *, struct cfdata *, void *));
60 static void pconsattach __P((struct device *, struct device *, void *));
61
62 struct cfattach pcons_ca = {
63 sizeof(struct pconssoftc), pconsmatch, pconsattach
64 };
65
66 extern struct cfdriver pcons_cd;
67 static struct cnm_state pcons_cnm_state;
68
69 static int pconsprobe __P((void));
70 extern struct consdev *cn_tab;
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, D_TTY
83 };
84
85 static int
86 pconsmatch(parent, match, aux)
87 struct device *parent;
88 struct cfdata *match;
89 void *aux;
90 {
91 struct mainbus_attach_args *ma = aux;
92 extern int prom_cngetc __P((dev_t));
93
94 /* Only attach if no other console has attached. */
95 return ((ma->ma_name != NULL) &&
96 (strcmp("pcons", ma->ma_name) == 0) &&
97 (cn_tab->cn_getc == prom_cngetc));
98
99 }
100
101 static void
102 pconsattach(parent, self, aux)
103 struct device *parent, *self;
104 void *aux;
105 {
106 struct pconssoftc *sc = (struct pconssoftc *) self;
107
108 printf("\n");
109 if (!pconsprobe())
110 return;
111
112 cn_init_magic(&pcons_cnm_state);
113 cn_set_magic("+++++");
114 callout_init(&sc->sc_poll_ch);
115 }
116
117 static void pconsstart __P((struct tty *));
118 static int pconsparam __P((struct tty *, struct termios *));
119 static void pcons_poll __P((void *));
120
121 int
122 pconsopen(dev, flag, mode, p)
123 dev_t dev;
124 int flag, mode;
125 struct proc *p;
126 {
127 struct pconssoftc *sc;
128 int unit = minor(dev);
129 struct tty *tp;
130
131 if (unit >= pcons_cd.cd_ndevs)
132 return ENXIO;
133 sc = pcons_cd.cd_devs[unit];
134 if (!sc)
135 return ENXIO;
136 if (!(tp = sc->of_tty))
137 sc->of_tty = tp = ttymalloc();
138 tp->t_oproc = pconsstart;
139 tp->t_param = pconsparam;
140 tp->t_dev = dev;
141 cn_tab->cn_dev = dev;
142 if (!(tp->t_state & TS_ISOPEN)) {
143 ttychars(tp);
144 tp->t_iflag = TTYDEF_IFLAG;
145 tp->t_oflag = TTYDEF_OFLAG;
146 tp->t_cflag = TTYDEF_CFLAG;
147 tp->t_lflag = TTYDEF_LFLAG;
148 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
149 pconsparam(tp, &tp->t_termios);
150 ttsetwater(tp);
151 } else if ((tp->t_state&TS_XCLUDE) && suser(p->p_ucred, &p->p_acflag))
152 return EBUSY;
153 tp->t_state |= TS_CARR_ON;
154
155 if (!(sc->of_flags & OFPOLL)) {
156 sc->of_flags |= OFPOLL;
157 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
158 }
159
160 return (*tp->t_linesw->l_open)(dev, tp);
161 }
162
163 int
164 pconsclose(dev, flag, mode, p)
165 dev_t dev;
166 int flag, mode;
167 struct proc *p;
168 {
169 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
170 struct tty *tp = sc->of_tty;
171
172 callout_stop(&sc->sc_poll_ch);
173 sc->of_flags &= ~OFPOLL;
174 (*tp->t_linesw->l_close)(tp, flag);
175 ttyclose(tp);
176 return 0;
177 }
178
179 int
180 pconsread(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_read)(tp, uio, flag);
189 }
190
191 int
192 pconswrite(dev, uio, flag)
193 dev_t dev;
194 struct uio *uio;
195 int flag;
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_write)(tp, uio, flag);
201 }
202
203 int
204 pconspoll(dev, events, p)
205 dev_t dev;
206 int events;
207 struct proc *p;
208 {
209 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
210 struct tty *tp = sc->of_tty;
211
212 return ((*tp->t_linesw->l_poll)(tp, events, p));
213 }
214
215 int
216 pconsioctl(dev, cmd, data, flag, p)
217 dev_t dev;
218 u_long cmd;
219 caddr_t data;
220 int flag;
221 struct proc *p;
222 {
223 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
224 struct tty *tp = sc->of_tty;
225 int error;
226
227 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p)) >= 0)
228 return error;
229 if ((error = ttioctl(tp, cmd, data, flag, p)) >= 0)
230 return error;
231 return ENOTTY;
232 }
233
234 struct tty *
235 pconstty(dev)
236 dev_t dev;
237 {
238 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)];
239
240 return sc->of_tty;
241 }
242
243 void
244 pconsstop(tp, flag)
245 struct tty *tp;
246 int flag;
247 {
248 }
249
250 static void
251 pconsstart(tp)
252 struct tty *tp;
253 {
254 struct clist *cl;
255 int s, len;
256 u_char buf[OFBURSTLEN];
257
258 s = spltty();
259 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
260 splx(s);
261 return;
262 }
263 tp->t_state |= TS_BUSY;
264 splx(s);
265 cl = &tp->t_outq;
266 len = q_to_b(cl, buf, OFBURSTLEN);
267 prom_putstr(buf, len);
268 s = spltty();
269 tp->t_state &= ~TS_BUSY;
270 if (cl->c_cc) {
271 tp->t_state |= TS_TIMEOUT;
272 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp);
273 }
274 if (cl->c_cc <= tp->t_lowat) {
275 if (tp->t_state & TS_ASLEEP) {
276 tp->t_state &= ~TS_ASLEEP;
277 wakeup(cl);
278 }
279 selwakeup(&tp->t_wsel);
280 }
281 splx(s);
282 }
283
284 static int
285 pconsparam(tp, t)
286 struct tty *tp;
287 struct termios *t;
288 {
289 tp->t_ispeed = t->c_ispeed;
290 tp->t_ospeed = t->c_ospeed;
291 tp->t_cflag = t->c_cflag;
292 return 0;
293 }
294
295 static void
296 pcons_poll(aux)
297 void *aux;
298 {
299 struct pconssoftc *sc = aux;
300 struct tty *tp = sc->of_tty;
301 int c;
302 char ch;
303
304 while ((c = prom_peekchar()) >= 0) {
305 ch = c;
306 cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
307 if (tp && (tp->t_state & TS_ISOPEN))
308 (*tp->t_linesw->l_rint)(ch, tp);
309 }
310 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
311 }
312
313 int
314 pconsprobe()
315 {
316 switch (prom_version()) {
317 #ifdef PROM_OLDMON
318 case PROM_OLDMON:
319 case PROM_OBP_V0:
320 return (1);
321 #endif /* PROM_OLDMON */
322 #ifdef PROM_OBP_V2
323 case PROM_OBP_V2:
324 case PROM_OBP_V3:
325 case PROM_OPENFIRM:
326 return (prom_stdin() && prom_stdout());
327 #endif /* PROM_OBP_V2 */
328 default: break;
329 }
330 return (0);
331 }
332
333 void
334 pcons_cnpollc(dev, on)
335 dev_t dev;
336 int on;
337 {
338 struct pconssoftc *sc = NULL;
339
340 if (pcons_cd.cd_devs)
341 sc = pcons_cd.cd_devs[minor(dev)];
342
343 if (on) {
344 if (!sc) return;
345 if (sc->of_flags & OFPOLL)
346 callout_stop(&sc->sc_poll_ch);
347 sc->of_flags &= ~OFPOLL;
348 } else {
349 /* Resuming kernel. */
350 if (sc && !(sc->of_flags & OFPOLL)) {
351 sc->of_flags |= OFPOLL;
352 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
353 }
354 }
355 }
356
357 void pcons_dopoll __P((void));
358 void
359 pcons_dopoll() {
360 pcons_poll((void*)pcons_cd.cd_devs[0]);
361 }
362