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