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