1 /* $NetBSD: ofcons.c,v 1.27 2006/05/15 12:40:30 yamt Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 * Copyright (C) 1995, 1996 TooLs GmbH. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by TooLs GmbH. 19 * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ofcons.c,v 1.27 2006/05/15 12:40:30 yamt Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/conf.h> 39 #include <sys/device.h> 40 #include <sys/proc.h> 41 #include <sys/systm.h> 42 #include <sys/callout.h> 43 #include <sys/tty.h> 44 #include <sys/kauth.h> 45 46 #include <dev/cons.h> 47 48 #include <dev/ofw/openfirm.h> 49 50 struct ofcons_softc { 51 struct device of_dev; 52 struct tty *of_tty; 53 struct callout sc_poll_ch; 54 int of_flags; 55 }; 56 /* flags: */ 57 #define OFPOLL 1 58 59 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */ 60 61 cons_decl(ofcons_); 62 63 static int stdin, stdout; 64 65 static int ofcons_match(struct device *, struct cfdata *, void *); 66 static void ofcons_attach(struct device *, struct device *, void *); 67 68 CFATTACH_DECL(ofcons, sizeof(struct ofcons_softc), 69 ofcons_match, ofcons_attach, NULL, NULL); 70 71 extern struct cfdriver ofcons_cd; 72 73 dev_type_open(ofcons_open); 74 dev_type_close(ofcons_close); 75 dev_type_read(ofcons_read); 76 dev_type_write(ofcons_write); 77 dev_type_ioctl(ofcons_ioctl); 78 dev_type_tty(ofcons_tty); 79 dev_type_poll(ofcons_poll); 80 81 const struct cdevsw ofcons_cdevsw = { 82 ofcons_open, ofcons_close, ofcons_read, ofcons_write, ofcons_ioctl, 83 nostop, ofcons_tty, ofcons_poll, nommap, ttykqfilter, D_TTY 84 }; 85 86 static int ofcons_probe(void); 87 88 static int 89 ofcons_match(parent, match, aux) 90 struct device *parent; 91 struct cfdata *match; 92 void *aux; 93 { 94 struct ofbus_attach_args *oba = aux; 95 96 if (strcmp(oba->oba_busname, "ofw")) 97 return (0); 98 if (!ofcons_probe()) 99 return 0; 100 return OF_instance_to_package(stdin) == oba->oba_phandle 101 || OF_instance_to_package(stdout) == oba->oba_phandle; 102 } 103 104 static void 105 ofcons_attach(parent, self, aux) 106 struct device *parent, *self; 107 void *aux; 108 { 109 struct ofcons_softc *sc = device_private(self); 110 111 printf("\n"); 112 113 callout_init(&sc->sc_poll_ch); 114 } 115 116 static void ofcons_start(struct tty *); 117 static int ofcons_param(struct tty *, struct termios *); 118 static void ofcons_pollin(void *); 119 120 int 121 ofcons_open(dev, flag, mode, l) 122 dev_t dev; 123 int flag, mode; 124 struct lwp *l; 125 { 126 struct ofcons_softc *sc; 127 int unit = minor(dev); 128 struct tty *tp; 129 130 if (unit >= ofcons_cd.cd_ndevs) 131 return ENXIO; 132 sc = ofcons_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 = ofcons_start; 138 tp->t_param = ofcons_param; 139 tp->t_dev = dev; 140 if (!(tp->t_state & TS_ISOPEN)) { 141 ttychars(tp); 142 tp->t_iflag = TTYDEF_IFLAG; 143 tp->t_oflag = TTYDEF_OFLAG; 144 tp->t_cflag = TTYDEF_CFLAG; 145 tp->t_lflag = TTYDEF_LFLAG; 146 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 147 ofcons_param(tp, &tp->t_termios); 148 ttsetwater(tp); 149 } else if ((tp->t_state&TS_XCLUDE) && 150 kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, 151 &l->l_proc->p_acflag)) 152 return EBUSY; 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, ofcons_pollin, sc); 158 } 159 160 return (*tp->t_linesw->l_open)(dev, tp); 161 } 162 163 int 164 ofcons_close(dev, flag, mode, l) 165 dev_t dev; 166 int flag, mode; 167 struct lwp *l; 168 { 169 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 170 struct tty *tp = sc->of_tty; 171 172 callout_stop(&sc->sc_poll_ch); 173 sc->of_flags &= ~OFPOLL; 174 (*tp->t_linesw->l_close)(tp, flag); 175 ttyclose(tp); 176 return 0; 177 } 178 179 int 180 ofcons_read(dev, uio, flag) 181 dev_t dev; 182 struct uio *uio; 183 int flag; 184 { 185 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 186 struct tty *tp = sc->of_tty; 187 188 return (*tp->t_linesw->l_read)(tp, uio, flag); 189 } 190 191 int 192 ofcons_write(dev, uio, flag) 193 dev_t dev; 194 struct uio *uio; 195 int flag; 196 { 197 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 198 struct tty *tp = sc->of_tty; 199 200 return (*tp->t_linesw->l_write)(tp, uio, flag); 201 } 202 203 int 204 ofcons_poll(dev, events, l) 205 dev_t dev; 206 int events; 207 struct lwp *l; 208 { 209 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 210 struct tty *tp = sc->of_tty; 211 212 return ((*tp->t_linesw->l_poll)(tp, events, l)); 213 } 214 int 215 ofcons_ioctl(dev, cmd, data, flag, l) 216 dev_t dev; 217 u_long cmd; 218 caddr_t data; 219 int flag; 220 struct lwp *l; 221 { 222 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 223 struct tty *tp = sc->of_tty; 224 int error; 225 226 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH) 227 return error; 228 return ttioctl(tp, cmd, data, flag, l); 229 } 230 231 struct tty * 232 ofcons_tty(dev) 233 dev_t dev; 234 { 235 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 236 237 return sc->of_tty; 238 } 239 240 static void 241 ofcons_start(tp) 242 struct tty *tp; 243 { 244 struct clist *cl; 245 int s, len; 246 u_char buf[OFBURSTLEN]; 247 248 s = spltty(); 249 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 250 splx(s); 251 return; 252 } 253 tp->t_state |= TS_BUSY; 254 splx(s); 255 cl = &tp->t_outq; 256 len = q_to_b(cl, buf, OFBURSTLEN); 257 OF_write(stdout, buf, len); 258 s = spltty(); 259 tp->t_state &= ~TS_BUSY; 260 if (cl->c_cc) { 261 tp->t_state |= TS_TIMEOUT; 262 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp); 263 } 264 if (cl->c_cc <= tp->t_lowat) { 265 if (tp->t_state & TS_ASLEEP) { 266 tp->t_state &= ~TS_ASLEEP; 267 wakeup(cl); 268 } 269 selwakeup(&tp->t_wsel); 270 } 271 splx(s); 272 } 273 274 static int 275 ofcons_param(tp, t) 276 struct tty *tp; 277 struct termios *t; 278 { 279 tp->t_ispeed = t->c_ispeed; 280 tp->t_ospeed = t->c_ospeed; 281 tp->t_cflag = t->c_cflag; 282 return 0; 283 } 284 285 static void 286 ofcons_pollin(aux) 287 void *aux; 288 { 289 struct ofcons_softc *sc = aux; 290 struct tty *tp = sc->of_tty; 291 char ch; 292 293 while (OF_read(stdin, &ch, 1) > 0) { 294 if (tp && (tp->t_state & TS_ISOPEN)) 295 (*tp->t_linesw->l_rint)(ch, tp); 296 } 297 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 298 } 299 300 static int 301 ofcons_probe() 302 { 303 int chosen; 304 char stdinbuf[4], stdoutbuf[4]; 305 306 if (stdin) 307 return 1; 308 if ((chosen = OF_finddevice("/chosen")) == -1) 309 return 0; 310 if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) != 311 sizeof stdinbuf || 312 OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) != 313 sizeof stdoutbuf) 314 return 0; 315 316 /* Decode properties. */ 317 stdin = of_decode_int(stdinbuf); 318 stdout = of_decode_int(stdoutbuf); 319 320 return 1; 321 } 322 323 void 324 ofcons_cnprobe(cd) 325 struct consdev *cd; 326 { 327 int maj; 328 329 if (!ofcons_probe()) 330 return; 331 332 maj = cdevsw_lookup_major(&ofcons_cdevsw); 333 cd->cn_dev = makedev(maj, 0); 334 cd->cn_pri = CN_INTERNAL; 335 } 336 337 void 338 ofcons_cninit(cd) 339 struct consdev *cd; 340 { 341 } 342 343 int 344 ofcons_cngetc(dev) 345 dev_t dev; 346 { 347 unsigned char ch = '\0'; 348 int l; 349 350 while ((l = OF_read(stdin, &ch, 1)) != 1) 351 if (l != -2 && l != 0) 352 return -1; 353 return ch; 354 } 355 356 void 357 ofcons_cnputc(dev, c) 358 dev_t dev; 359 int c; 360 { 361 char ch = c; 362 363 OF_write(stdout, &ch, 1); 364 } 365 366 void 367 ofcons_cnpollc(dev, on) 368 dev_t dev; 369 int on; 370 { 371 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 372 373 if (!sc) 374 return; 375 if (on) { 376 if (sc->of_flags & OFPOLL) 377 callout_stop(&sc->sc_poll_ch); 378 sc->of_flags &= ~OFPOLL; 379 } else { 380 if (!(sc->of_flags & OFPOLL)) { 381 sc->of_flags |= OFPOLL; 382 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 383 } 384 } 385 } 386