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