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