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