pcons.c revision 1.33.2.1 1 1.33.2.1 tls /* $NetBSD: pcons.c,v 1.33.2.1 2014/08/10 06:54:08 tls 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.33.2.1 tls __KERNEL_RCSID(0, "$NetBSD: pcons.c,v 1.33.2.1 2014/08/10 06:54:08 tls 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.31 christos static int pconsmatch(device_t, cfdata_t, void *);
65 1.31 christos static void pconsattach(device_t, device_t, void *);
66 1.1 eeh
67 1.31 christos CFATTACH_DECL_NEW(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.33 dholland .d_open = pconsopen,
82 1.33 dholland .d_close = pconsclose,
83 1.33 dholland .d_read = pconsread,
84 1.33 dholland .d_write = pconswrite,
85 1.33 dholland .d_ioctl = pconsioctl,
86 1.33 dholland .d_stop = nostop,
87 1.33 dholland .d_tty = pconstty,
88 1.33 dholland .d_poll = pconspoll,
89 1.33 dholland .d_mmap = nommap,
90 1.33 dholland .d_kqfilter = ttykqfilter,
91 1.33.2.1 tls .d_discard = nodiscard,
92 1.33 dholland .d_flag = D_TTY
93 1.10 gehenna };
94 1.10 gehenna
95 1.5 eeh static struct cnm_state pcons_cnm_state;
96 1.1 eeh
97 1.20 cdi static int pconsprobe(void);
98 1.5 eeh extern struct consdev *cn_tab;
99 1.1 eeh
100 1.1 eeh static int
101 1.31 christos pconsmatch(device_t parent, cfdata_t match, void *aux)
102 1.1 eeh {
103 1.1 eeh struct mainbus_attach_args *ma = aux;
104 1.20 cdi extern int prom_cngetc(dev_t);
105 1.1 eeh
106 1.1 eeh /* Only attach if no other console has attached. */
107 1.1 eeh return ((strcmp("pcons", ma->ma_name) == 0) &&
108 1.1 eeh (cn_tab->cn_getc == prom_cngetc));
109 1.1 eeh
110 1.1 eeh }
111 1.1 eeh
112 1.1 eeh static void
113 1.31 christos pconsattach(device_t parent, device_t self, void *aux)
114 1.1 eeh {
115 1.31 christos struct pconssoftc *sc = device_private(self);
116 1.31 christos sc->of_dev = self;
117 1.1 eeh
118 1.1 eeh printf("\n");
119 1.1 eeh if (!pconsprobe())
120 1.1 eeh return;
121 1.1 eeh
122 1.5 eeh cn_init_magic(&pcons_cnm_state);
123 1.5 eeh cn_set_magic("+++++");
124 1.25 ad callout_init(&sc->sc_poll_ch, 0);
125 1.1 eeh }
126 1.1 eeh
127 1.20 cdi static void pconsstart(struct tty *);
128 1.20 cdi static int pconsparam(struct tty *, struct termios *);
129 1.20 cdi static void pcons_poll(void *);
130 1.1 eeh
131 1.1 eeh int
132 1.20 cdi pconsopen(dev_t dev, int flag, int mode, struct lwp *l)
133 1.1 eeh {
134 1.1 eeh struct pconssoftc *sc;
135 1.1 eeh struct tty *tp;
136 1.1 eeh
137 1.29 cegger sc = device_lookup_private(&pcons_cd, minor(dev));
138 1.1 eeh if (!sc)
139 1.1 eeh return ENXIO;
140 1.1 eeh if (!(tp = sc->of_tty))
141 1.30 rmind sc->of_tty = tp = tty_alloc();
142 1.1 eeh tp->t_oproc = pconsstart;
143 1.1 eeh tp->t_param = pconsparam;
144 1.1 eeh tp->t_dev = dev;
145 1.5 eeh cn_tab->cn_dev = dev;
146 1.23 elad if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
147 1.23 elad return (EBUSY);
148 1.1 eeh if (!(tp->t_state & TS_ISOPEN)) {
149 1.1 eeh ttychars(tp);
150 1.1 eeh tp->t_iflag = TTYDEF_IFLAG;
151 1.1 eeh tp->t_oflag = TTYDEF_OFLAG;
152 1.1 eeh tp->t_cflag = TTYDEF_CFLAG;
153 1.1 eeh tp->t_lflag = TTYDEF_LFLAG;
154 1.1 eeh tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
155 1.1 eeh pconsparam(tp, &tp->t_termios);
156 1.1 eeh ttsetwater(tp);
157 1.23 elad }
158 1.1 eeh tp->t_state |= TS_CARR_ON;
159 1.1 eeh
160 1.1 eeh if (!(sc->of_flags & OFPOLL)) {
161 1.1 eeh sc->of_flags |= OFPOLL;
162 1.1 eeh callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
163 1.1 eeh }
164 1.1 eeh
165 1.4 eeh return (*tp->t_linesw->l_open)(dev, tp);
166 1.1 eeh }
167 1.1 eeh
168 1.1 eeh int
169 1.20 cdi pconsclose(dev_t dev, int flag, int mode, struct lwp *l)
170 1.1 eeh {
171 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd,minor(dev));
172 1.1 eeh struct tty *tp = sc->of_tty;
173 1.1 eeh
174 1.1 eeh callout_stop(&sc->sc_poll_ch);
175 1.1 eeh sc->of_flags &= ~OFPOLL;
176 1.4 eeh (*tp->t_linesw->l_close)(tp, flag);
177 1.1 eeh ttyclose(tp);
178 1.1 eeh return 0;
179 1.1 eeh }
180 1.1 eeh
181 1.1 eeh int
182 1.20 cdi pconsread(dev_t dev, struct uio *uio, int flag)
183 1.1 eeh {
184 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
185 1.1 eeh struct tty *tp = sc->of_tty;
186 1.1 eeh
187 1.4 eeh return (*tp->t_linesw->l_read)(tp, uio, flag);
188 1.1 eeh }
189 1.1 eeh
190 1.1 eeh int
191 1.20 cdi pconswrite(dev_t dev, struct uio *uio, int flag)
192 1.1 eeh {
193 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
194 1.1 eeh struct tty *tp = sc->of_tty;
195 1.1 eeh
196 1.4 eeh return (*tp->t_linesw->l_write)(tp, uio, flag);
197 1.7 scw }
198 1.7 scw
199 1.7 scw int
200 1.20 cdi pconspoll(dev_t dev, int events, struct lwp *l)
201 1.7 scw {
202 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
203 1.7 scw struct tty *tp = sc->of_tty;
204 1.7 scw
205 1.19 christos return ((*tp->t_linesw->l_poll)(tp, events, l));
206 1.1 eeh }
207 1.1 eeh
208 1.1 eeh int
209 1.24 christos pconsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
210 1.1 eeh {
211 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
212 1.1 eeh struct tty *tp = sc->of_tty;
213 1.1 eeh int error;
214 1.1 eeh
215 1.19 christos if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH)
216 1.1 eeh return error;
217 1.19 christos return ttioctl(tp, cmd, data, flag, l);
218 1.1 eeh }
219 1.1 eeh
220 1.1 eeh struct tty *
221 1.20 cdi pconstty(dev_t dev)
222 1.1 eeh {
223 1.29 cegger struct pconssoftc *sc = device_lookup_private(&pcons_cd, minor(dev));
224 1.1 eeh
225 1.1 eeh return sc->of_tty;
226 1.1 eeh }
227 1.1 eeh
228 1.1 eeh static void
229 1.20 cdi pconsstart(struct tty *tp)
230 1.1 eeh {
231 1.1 eeh struct clist *cl;
232 1.1 eeh int s, len;
233 1.1 eeh u_char buf[OFBURSTLEN];
234 1.1 eeh
235 1.1 eeh s = spltty();
236 1.1 eeh if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
237 1.1 eeh splx(s);
238 1.1 eeh return;
239 1.1 eeh }
240 1.1 eeh tp->t_state |= TS_BUSY;
241 1.1 eeh splx(s);
242 1.1 eeh cl = &tp->t_outq;
243 1.1 eeh len = q_to_b(cl, buf, OFBURSTLEN);
244 1.18 pk prom_write(prom_stdout(), buf, len);
245 1.1 eeh s = spltty();
246 1.1 eeh tp->t_state &= ~TS_BUSY;
247 1.28 ad if (ttypull(tp)) {
248 1.1 eeh tp->t_state |= TS_TIMEOUT;
249 1.27 joerg callout_schedule(&tp->t_rstrt_ch, 1);
250 1.1 eeh }
251 1.1 eeh splx(s);
252 1.1 eeh }
253 1.1 eeh
254 1.1 eeh static int
255 1.20 cdi pconsparam(struct tty *tp, struct termios *t)
256 1.1 eeh {
257 1.1 eeh tp->t_ispeed = t->c_ispeed;
258 1.1 eeh tp->t_ospeed = t->c_ospeed;
259 1.1 eeh tp->t_cflag = t->c_cflag;
260 1.1 eeh return 0;
261 1.1 eeh }
262 1.1 eeh
263 1.1 eeh static void
264 1.20 cdi pcons_poll(void *aux)
265 1.1 eeh {
266 1.1 eeh struct pconssoftc *sc = aux;
267 1.1 eeh struct tty *tp = sc->of_tty;
268 1.1 eeh char ch;
269 1.1 eeh
270 1.18 pk while (prom_read(prom_stdin(), &ch, 1) > 0) {
271 1.5 eeh cn_check_magic(tp->t_dev, ch, pcons_cnm_state);
272 1.1 eeh if (tp && (tp->t_state & TS_ISOPEN))
273 1.4 eeh (*tp->t_linesw->l_rint)(ch, tp);
274 1.1 eeh }
275 1.1 eeh callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
276 1.1 eeh }
277 1.1 eeh
278 1.1 eeh int
279 1.29 cegger pconsprobe(void)
280 1.1 eeh {
281 1.1 eeh
282 1.18 pk return (prom_stdin() && prom_stdout());
283 1.1 eeh }
284 1.1 eeh
285 1.1 eeh void
286 1.20 cdi pcons_cnpollc(dev_t dev, int on)
287 1.1 eeh {
288 1.29 cegger struct pconssoftc *sc;
289 1.1 eeh
290 1.29 cegger sc = device_lookup_private(&pcons_cd, minor(dev));
291 1.29 cegger if (sc == NULL)
292 1.29 cegger return;
293 1.1 eeh
294 1.1 eeh if (on) {
295 1.1 eeh if (sc->of_flags & OFPOLL)
296 1.1 eeh callout_stop(&sc->sc_poll_ch);
297 1.1 eeh sc->of_flags &= ~OFPOLL;
298 1.1 eeh } else {
299 1.1 eeh /* Resuming kernel. */
300 1.29 cegger if (!(sc->of_flags & OFPOLL)) {
301 1.1 eeh sc->of_flags |= OFPOLL;
302 1.1 eeh callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc);
303 1.1 eeh }
304 1.1 eeh }
305 1.3 eeh }
306 1.3 eeh
307 1.20 cdi void pcons_dopoll(void);
308 1.3 eeh void
309 1.29 cegger pcons_dopoll(void)
310 1.29 cegger {
311 1.29 cegger pcons_poll(device_lookup_private(&pcons_cd, 0));
312 1.1 eeh }
313