1 /* $NetBSD: pcons.c,v 1.10 2006/05/14 21:57:13 elad 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.10 2006/05/14 21:57:13 elad 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/promlib.h> 56 #include <machine/cpu.h> 57 #include <machine/psl.h> 58 59 #include <dev/cons.h> 60 61 #include <sun2/dev/cons.h> 62 63 static int pconsmatch(struct device *, struct cfdata *, void *); 64 static void pconsattach(struct device *, struct device *, void *); 65 66 CFATTACH_DECL(pcons, sizeof(struct pconssoftc), 67 pconsmatch, pconsattach, NULL, NULL); 68 69 extern struct cfdriver pcons_cd; 70 static struct cnm_state pcons_cnm_state; 71 72 static int pconsprobe(void); 73 extern struct consdev *cn_tab; 74 75 dev_type_open(pconsopen); 76 dev_type_close(pconsclose); 77 dev_type_read(pconsread); 78 dev_type_write(pconswrite); 79 dev_type_ioctl(pconsioctl); 80 dev_type_tty(pconstty); 81 dev_type_poll(pconspoll); 82 83 const struct cdevsw pcons_cdevsw = { 84 pconsopen, pconsclose, pconsread, pconswrite, pconsioctl, 85 nostop, pconstty, pconspoll, nommap, ttykqfilter, D_TTY 86 }; 87 88 static int 89 pconsmatch(struct device *parent, struct cfdata *match, void *aux) 90 { 91 struct mainbus_attach_args *ma = aux; 92 extern int prom_cngetc(dev_t); 93 94 /* Only attach if no other console has attached. */ 95 return ((ma->ma_name != NULL) && 96 (strcmp("pcons", ma->ma_name) == 0) && 97 (cn_tab->cn_getc == prom_cngetc)); 98 99 } 100 101 static void 102 pconsattach(struct device *parent, struct device *self, void *aux) 103 { 104 struct pconssoftc *sc = (struct pconssoftc *) self; 105 106 printf("\n"); 107 if (!pconsprobe()) 108 return; 109 110 cn_init_magic(&pcons_cnm_state); 111 cn_set_magic("+++++"); 112 callout_init(&sc->sc_poll_ch); 113 } 114 115 static void pconsstart(struct tty *); 116 static int pconsparam(struct tty *, struct termios *); 117 static void pcons_poll(void *); 118 119 int 120 pconsopen(dev_t dev, int flag, int mode, struct lwp *l) 121 { 122 struct pconssoftc *sc; 123 int unit = minor(dev); 124 struct tty *tp; 125 126 if (unit >= pcons_cd.cd_ndevs) 127 return ENXIO; 128 sc = pcons_cd.cd_devs[unit]; 129 if (!sc) 130 return ENXIO; 131 if (!(tp = sc->of_tty)) 132 sc->of_tty = tp = ttymalloc(); 133 tp->t_oproc = pconsstart; 134 tp->t_param = pconsparam; 135 tp->t_dev = dev; 136 cn_tab->cn_dev = dev; 137 if (!(tp->t_state & TS_ISOPEN)) { 138 ttychars(tp); 139 tp->t_iflag = TTYDEF_IFLAG; 140 tp->t_oflag = TTYDEF_OFLAG; 141 tp->t_cflag = TTYDEF_CFLAG; 142 tp->t_lflag = TTYDEF_LFLAG; 143 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 144 pconsparam(tp, &tp->t_termios); 145 ttsetwater(tp); 146 } else if ((tp->t_state&TS_XCLUDE) && 147 kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag)) 148 return EBUSY; 149 tp->t_state |= TS_CARR_ON; 150 151 if (!(sc->of_flags & OFPOLL)) { 152 sc->of_flags |= OFPOLL; 153 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 154 } 155 156 return (*tp->t_linesw->l_open)(dev, tp); 157 } 158 159 int 160 pconsclose(dev_t dev, int flag, int mode, struct lwp *l) 161 { 162 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 163 struct tty *tp = sc->of_tty; 164 165 callout_stop(&sc->sc_poll_ch); 166 sc->of_flags &= ~OFPOLL; 167 (*tp->t_linesw->l_close)(tp, flag); 168 ttyclose(tp); 169 return 0; 170 } 171 172 int 173 pconsread(dev_t dev, struct uio *uio, int flag) 174 { 175 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 176 struct tty *tp = sc->of_tty; 177 178 return (*tp->t_linesw->l_read)(tp, uio, flag); 179 } 180 181 int 182 pconswrite(dev_t dev, struct uio *uio, int flag) 183 { 184 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 185 struct tty *tp = sc->of_tty; 186 187 return (*tp->t_linesw->l_write)(tp, uio, flag); 188 } 189 190 int 191 pconspoll(dev_t dev, int events, struct lwp *l) 192 { 193 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 194 struct tty *tp = sc->of_tty; 195 196 return ((*tp->t_linesw->l_poll)(tp, events, l)); 197 } 198 199 int 200 pconsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l) 201 { 202 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 203 struct tty *tp = sc->of_tty; 204 int error; 205 206 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) >= 0) 207 return error; 208 if ((error = ttioctl(tp, cmd, data, flag, l)) >= 0) 209 return error; 210 return ENOTTY; 211 } 212 213 struct tty * 214 pconstty(dev_t dev) 215 { 216 struct pconssoftc *sc = pcons_cd.cd_devs[minor(dev)]; 217 218 return sc->of_tty; 219 } 220 221 static void 222 pconsstart(struct tty *tp) 223 { 224 struct clist *cl; 225 int s, len; 226 u_char buf[OFBURSTLEN]; 227 228 s = spltty(); 229 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 230 splx(s); 231 return; 232 } 233 tp->t_state |= TS_BUSY; 234 splx(s); 235 cl = &tp->t_outq; 236 len = q_to_b(cl, buf, OFBURSTLEN); 237 prom_putstr(buf, len); 238 s = spltty(); 239 tp->t_state &= ~TS_BUSY; 240 if (cl->c_cc) { 241 tp->t_state |= TS_TIMEOUT; 242 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp); 243 } 244 if (cl->c_cc <= tp->t_lowat) { 245 if (tp->t_state & TS_ASLEEP) { 246 tp->t_state &= ~TS_ASLEEP; 247 wakeup(cl); 248 } 249 selwakeup(&tp->t_wsel); 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 int c; 269 char ch; 270 271 while ((c = prom_peekchar()) >= 0) { 272 ch = c; 273 cn_check_magic(tp->t_dev, ch, pcons_cnm_state); 274 if (tp && (tp->t_state & TS_ISOPEN)) 275 (*tp->t_linesw->l_rint)(ch, tp); 276 } 277 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 278 } 279 280 int 281 pconsprobe(void) 282 { 283 switch (prom_version()) { 284 #ifdef PROM_OLDMON 285 case PROM_OLDMON: 286 case PROM_OBP_V0: 287 return (1); 288 #endif /* PROM_OLDMON */ 289 #ifdef PROM_OBP_V2 290 case PROM_OBP_V2: 291 case PROM_OBP_V3: 292 case PROM_OPENFIRM: 293 return (prom_stdin() && prom_stdout()); 294 #endif /* PROM_OBP_V2 */ 295 default: break; 296 } 297 return (0); 298 } 299 300 void 301 pcons_cnpollc(dev_t dev, int on) 302 { 303 struct pconssoftc *sc = NULL; 304 305 if (pcons_cd.cd_devs) 306 sc = pcons_cd.cd_devs[minor(dev)]; 307 308 if (on) { 309 if (!sc) return; 310 if (sc->of_flags & OFPOLL) 311 callout_stop(&sc->sc_poll_ch); 312 sc->of_flags &= ~OFPOLL; 313 } else { 314 /* Resuming kernel. */ 315 if (sc && !(sc->of_flags & OFPOLL)) { 316 sc->of_flags |= OFPOLL; 317 callout_reset(&sc->sc_poll_ch, 1, pcons_poll, sc); 318 } 319 } 320 } 321 322 void pcons_dopoll(void); 323 void 324 pcons_dopoll(void) 325 { 326 pcons_poll((void*)pcons_cd.cd_devs[0]); 327 } 328