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