ttycons.c revision 1.12 1 /* $NetBSD: ttycons.c,v 1.12 2011/12/15 01:04:15 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ttycons.c,v 1.12 2011/12/15 01:04:15 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/proc.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kauth.h>
38 #include <sys/termios.h>
39 #include <sys/tty.h>
40
41 #include <dev/cons.h>
42
43 #include <machine/mainbus.h>
44 #include <machine/thunk.h>
45
46 static int ttycons_match(device_t, cfdata_t, void *);
47 static void ttycons_attach(device_t, device_t, void *);
48
49 void ttycons_consinit(void);
50
51 struct ttycons_softc {
52 device_t sc_dev;
53 struct tty *sc_tty;
54 void *sc_rd_sih;
55 void *sc_ctrlc_sih;
56 };
57
58 dev_type_cngetc(ttycons_cngetc);
59 dev_type_cnputc(ttycons_cnputc);
60 dev_type_cnpollc(ttycons_cnpollc);
61
62 static struct cnm_state ttycons_cnm_state;
63 struct consdev ttycons_consdev = {
64 .cn_getc = ttycons_cngetc,
65 .cn_putc = ttycons_cnputc,
66 .cn_pollc = ttycons_cnpollc,
67 .cn_dev = NODEV,
68 .cn_pri = CN_NORMAL,
69 };
70
71 CFATTACH_DECL_NEW(ttycons, sizeof(struct ttycons_softc),
72 ttycons_match, ttycons_attach, NULL, NULL);
73
74 extern struct cfdriver ttycons_cd;
75
76 dev_type_open(ttycons_open);
77 dev_type_close(ttycons_close);
78 dev_type_read(ttycons_read);
79 dev_type_write(ttycons_write);
80 dev_type_ioctl(ttycons_ioctl);
81 dev_type_stop(ttycons_stop);
82 dev_type_tty(ttycons_tty);
83 dev_type_poll(ttycons_poll);
84
85 const struct cdevsw ttycons_cdevsw = {
86 .d_open = ttycons_open,
87 .d_close = ttycons_close,
88 .d_read = ttycons_read,
89 .d_write = ttycons_write,
90 .d_ioctl = ttycons_ioctl,
91 .d_stop = ttycons_stop,
92 .d_tty = ttycons_tty,
93 .d_poll = ttycons_poll,
94 .d_kqfilter = ttykqfilter,
95 .d_flag = D_TTY,
96 };
97
98 static void ttycons_start(struct tty *);
99 static int ttycons_param(struct tty *, struct termios *);
100
101 static void ttycons_intr(int);
102 static void ttycons_softintr(void *);
103
104 static void ttycons_ctrlc(int);
105 static void ttycons_softctrlc(void *);
106
107 static int
108 ttycons_match(device_t parent, cfdata_t match, void *opaque)
109 {
110 struct thunkbus_attach_args *taa = opaque;
111
112 if (taa->taa_type != THUNKBUS_TYPE_TTYCONS)
113 return 0;
114
115 return 1;
116 }
117
118 static void
119 ttycons_attach(device_t parent, device_t self, void *opaque)
120 {
121 struct ttycons_softc *sc = device_private(self);
122 int maj;
123
124 aprint_naive("\n");
125 aprint_normal(": console\n");
126
127 sc->sc_dev = self;
128 sc->sc_tty = tty_alloc();
129 tty_attach(sc->sc_tty);
130 sc->sc_tty->t_oproc = ttycons_start;
131 sc->sc_tty->t_param = ttycons_param;
132
133 maj = cdevsw_lookup_major(&ttycons_cdevsw);
134 cn_tab->cn_dev = makedev(maj, device_unit(self));
135 sc->sc_tty->t_dev = cn_tab->cn_dev;
136
137 sc->sc_rd_sih = softint_establish(SOFTINT_SERIAL,
138 ttycons_softintr, sc);
139 if (sc->sc_rd_sih == NULL)
140 panic("couldn't establish ttycons intr handler\n");
141
142 sc->sc_ctrlc_sih = softint_establish(SOFTINT_SERIAL,
143 ttycons_softctrlc, sc);
144 if (sc->sc_ctrlc_sih == NULL)
145 panic("couldn't establish ttycons ctrlc handler\n");
146
147 thunk_signal(SIGIO, ttycons_intr);
148 thunk_signal(SIGINT, ttycons_ctrlc);
149 if (thunk_set_stdin_sigio(true) != 0)
150 panic("couldn't enable stdin async mode");
151 }
152
153 void
154 ttycons_consinit(void)
155 {
156 struct thunk_termios t;
157
158 thunk_tcgetattr(0, &t);
159 t.c_lflag &= ~(ECHO|ICANON);
160 t.c_cc[VTIME] = 0;
161 t.c_cc[VMIN] = 1;
162 thunk_tcsetattr(0, TCSANOW, &t);
163
164 cn_tab = &ttycons_consdev;
165 cn_init_magic(&ttycons_cnm_state);
166 cn_set_magic("\047\001");
167 }
168
169 int
170 ttycons_cngetc(dev_t dev)
171 {
172 return thunk_getchar();
173 }
174
175 void
176 ttycons_cnputc(dev_t dev, int c)
177 {
178 thunk_putchar(c);
179 }
180
181 void
182 ttycons_cnpollc(dev_t dev, int on)
183 {
184 }
185
186 int
187 ttycons_open(dev_t dev, int flag, int mode, lwp_t *l)
188 {
189 struct ttycons_softc *sc;
190 struct tty *t;
191
192 sc = device_lookup_private(&ttycons_cd, minor(dev));
193 if (sc == NULL)
194 return ENXIO;
195 t = sc->sc_tty;
196
197 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, t))
198 return EBUSY;
199
200 if ((t->t_state & TS_ISOPEN) == 0 && t->t_wopen == 0) {
201 t->t_dev = dev;
202 ttychars(t);
203 t->t_iflag = TTYDEF_IFLAG;
204 t->t_oflag = TTYDEF_OFLAG;
205 t->t_cflag = TTYDEF_CFLAG;
206 t->t_lflag = TTYDEF_LFLAG;
207 t->t_ispeed = t->t_ospeed = TTYDEF_SPEED;
208 ttycons_param(t, &t->t_termios);
209 ttsetwater(t);
210 }
211 t->t_state |= TS_CARR_ON;
212
213 return t->t_linesw->l_open(dev, t);
214 }
215
216 int
217 ttycons_close(dev_t dev, int flag, int mode, lwp_t *l)
218 {
219 struct ttycons_softc *sc;
220 struct tty *t;
221
222 sc = device_lookup_private(&ttycons_cd, minor(dev));
223 t = sc->sc_tty;
224
225 if (t == NULL)
226 return 0;
227
228 t->t_linesw->l_close(t, flag);
229 ttyclose(t);
230
231 return 0;
232 }
233
234 int
235 ttycons_read(dev_t dev, struct uio *uio, int flag)
236 {
237 struct ttycons_softc *sc;
238 struct tty *t;
239
240 sc = device_lookup_private(&ttycons_cd, minor(dev));
241 t = sc->sc_tty;
242
243 return t->t_linesw->l_read(t, uio, flag);
244 }
245
246 int
247 ttycons_write(dev_t dev, struct uio *uio, int flag)
248 {
249 struct ttycons_softc *sc;
250 struct tty *t;
251
252 sc = device_lookup_private(&ttycons_cd, minor(dev));
253 t = sc->sc_tty;
254
255 return t->t_linesw->l_write(t, uio, flag);
256 }
257
258 int
259 ttycons_poll(dev_t dev, int events, lwp_t *l)
260 {
261 struct ttycons_softc *sc;
262 struct tty *t;
263
264 sc = device_lookup_private(&ttycons_cd, minor(dev));
265 t = sc->sc_tty;
266
267 return t->t_linesw->l_poll(t, events, l);
268 }
269
270 struct tty *
271 ttycons_tty(dev_t dev)
272 {
273 struct ttycons_softc *sc;
274
275 sc = device_lookup_private(&ttycons_cd, minor(dev));
276
277 return sc->sc_tty;
278 }
279
280 int
281 ttycons_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
282 {
283 struct ttycons_softc *sc;
284 struct tty *t;
285 int error;
286
287 sc = device_lookup_private(&ttycons_cd, minor(dev));
288 t = sc->sc_tty;
289
290 error = t->t_linesw->l_ioctl(t, cmd, data, flag, l);
291 if (error != EPASSTHROUGH)
292 return error;
293
294 error = ttioctl(t, cmd, data, flag, l);
295 if (error != EPASSTHROUGH)
296 return error;
297
298 return EPASSTHROUGH;
299 }
300
301 static void
302 ttycons_start(struct tty *t)
303 {
304 u_char buf[80+1];
305 int s, len; //, i;
306
307 s = spltty();
308 if (t->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
309 splx(s);
310 return;
311 }
312 t->t_state |= TS_BUSY;
313 splx(s);
314
315 len = q_to_b(&t->t_outq, buf, sizeof(buf) - 1);
316 thunk_write(1, buf, len);
317
318 s = spltty();
319 t->t_state &= ~TS_BUSY;
320 if (ttypull(t)) {
321 t->t_state |= TS_TIMEOUT;
322 callout_schedule(&t->t_rstrt_ch, 1);
323 }
324 splx(s);
325 }
326
327 void
328 ttycons_stop(struct tty *t, int flag)
329 {
330 }
331
332 static int
333 ttycons_param(struct tty *t, struct termios *c)
334 {
335 t->t_ispeed = c->c_ispeed;
336 t->t_ospeed = c->c_ospeed;
337 t->t_cflag = c->c_cflag;
338 return 0;
339 }
340
341 static void
342 ttycons_intr(int sig)
343 {
344 struct ttycons_softc *sc;
345
346 curcpu()->ci_idepth++;
347 sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
348 if (sc) {
349 spl_intr(IPL_SERIAL, softint_schedule, sc->sc_rd_sih);
350 }
351 curcpu()->ci_idepth--;
352
353 }
354
355 static void
356 ttycons_softintr(void *priv)
357 {
358 struct ttycons_softc *sc = priv;
359 struct tty *t = sc->sc_tty;
360 unsigned char ch;
361 int c;
362
363 while ((c = thunk_pollchar()) >= 0) {
364 ch = (unsigned char)c;
365 cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
366 t->t_linesw->l_rint(ch, t);
367 }
368 }
369
370 static void
371 ttycons_ctrlc(int sig)
372 {
373 struct ttycons_softc *sc;
374
375 curcpu()->ci_idepth++;
376 sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
377 if (sc) {
378 spl_intr(IPL_SERIAL, softint_schedule, sc->sc_ctrlc_sih);
379 }
380 curcpu()->ci_idepth--;
381
382 }
383
384 static void
385 ttycons_softctrlc(void *priv)
386 {
387 struct ttycons_softc *sc = priv;
388 struct tty *t = sc->sc_tty;
389 unsigned char ch = 3; /* ETX */
390
391 cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
392 t->t_linesw->l_rint(ch, t);
393 }
394