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