1 /* $NetBSD: ofcons.c,v 1.29 2006/10/01 19:28:44 elad 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.29 2006/10/01 19:28:44 elad 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 141 return (EBUSY); 142 if (!(tp->t_state & TS_ISOPEN)) { 143 ttychars(tp); 144 tp->t_iflag = TTYDEF_IFLAG; 145 tp->t_oflag = TTYDEF_OFLAG; 146 tp->t_cflag = TTYDEF_CFLAG; 147 tp->t_lflag = TTYDEF_LFLAG; 148 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 149 ofcons_param(tp, &tp->t_termios); 150 ttsetwater(tp); 151 } 152 tp->t_state |= TS_CARR_ON; 153 154 if (!(sc->of_flags & OFPOLL)) { 155 sc->of_flags |= OFPOLL; 156 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 157 } 158 159 return (*tp->t_linesw->l_open)(dev, tp); 160 } 161 162 int 163 ofcons_close(dev, flag, mode, l) 164 dev_t dev; 165 int flag, mode; 166 struct lwp *l; 167 { 168 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 169 struct tty *tp = sc->of_tty; 170 171 callout_stop(&sc->sc_poll_ch); 172 sc->of_flags &= ~OFPOLL; 173 (*tp->t_linesw->l_close)(tp, flag); 174 ttyclose(tp); 175 return 0; 176 } 177 178 int 179 ofcons_read(dev, uio, flag) 180 dev_t dev; 181 struct uio *uio; 182 int flag; 183 { 184 struct ofcons_softc *sc = ofcons_cd.cd_devs[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 ofcons_write(dev, uio, flag) 192 dev_t dev; 193 struct uio *uio; 194 int flag; 195 { 196 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 197 struct tty *tp = sc->of_tty; 198 199 return (*tp->t_linesw->l_write)(tp, uio, flag); 200 } 201 202 int 203 ofcons_poll(dev, events, l) 204 dev_t dev; 205 int events; 206 struct lwp *l; 207 { 208 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 209 struct tty *tp = sc->of_tty; 210 211 return ((*tp->t_linesw->l_poll)(tp, events, l)); 212 } 213 int 214 ofcons_ioctl(dev, cmd, data, flag, l) 215 dev_t dev; 216 u_long cmd; 217 caddr_t data; 218 int flag; 219 struct lwp *l; 220 { 221 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 222 struct tty *tp = sc->of_tty; 223 int error; 224 225 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH) 226 return error; 227 return ttioctl(tp, cmd, data, flag, l); 228 } 229 230 struct tty * 231 ofcons_tty(dev) 232 dev_t dev; 233 { 234 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 235 236 return sc->of_tty; 237 } 238 239 static void 240 ofcons_start(tp) 241 struct tty *tp; 242 { 243 struct clist *cl; 244 int s, len; 245 u_char buf[OFBURSTLEN]; 246 247 s = spltty(); 248 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 249 splx(s); 250 return; 251 } 252 tp->t_state |= TS_BUSY; 253 splx(s); 254 cl = &tp->t_outq; 255 len = q_to_b(cl, buf, OFBURSTLEN); 256 OF_write(stdout, buf, len); 257 s = spltty(); 258 tp->t_state &= ~TS_BUSY; 259 if (cl->c_cc) { 260 tp->t_state |= TS_TIMEOUT; 261 callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, (void *)tp); 262 } 263 if (cl->c_cc <= tp->t_lowat) { 264 if (tp->t_state & TS_ASLEEP) { 265 tp->t_state &= ~TS_ASLEEP; 266 wakeup(cl); 267 } 268 selwakeup(&tp->t_wsel); 269 } 270 splx(s); 271 } 272 273 static int 274 ofcons_param(tp, t) 275 struct tty *tp; 276 struct termios *t; 277 { 278 tp->t_ispeed = t->c_ispeed; 279 tp->t_ospeed = t->c_ospeed; 280 tp->t_cflag = t->c_cflag; 281 return 0; 282 } 283 284 static void 285 ofcons_pollin(aux) 286 void *aux; 287 { 288 struct ofcons_softc *sc = aux; 289 struct tty *tp = sc->of_tty; 290 char ch; 291 292 while (OF_read(stdin, &ch, 1) > 0) { 293 if (tp && (tp->t_state & TS_ISOPEN)) 294 (*tp->t_linesw->l_rint)(ch, tp); 295 } 296 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 297 } 298 299 static int 300 ofcons_probe() 301 { 302 int chosen; 303 char stdinbuf[4], stdoutbuf[4]; 304 305 if (stdin) 306 return 1; 307 if ((chosen = OF_finddevice("/chosen")) == -1) 308 return 0; 309 if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) != 310 sizeof stdinbuf || 311 OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) != 312 sizeof stdoutbuf) 313 return 0; 314 315 /* Decode properties. */ 316 stdin = of_decode_int(stdinbuf); 317 stdout = of_decode_int(stdoutbuf); 318 319 return 1; 320 } 321 322 void 323 ofcons_cnprobe(cd) 324 struct consdev *cd; 325 { 326 int maj; 327 328 if (!ofcons_probe()) 329 return; 330 331 maj = cdevsw_lookup_major(&ofcons_cdevsw); 332 cd->cn_dev = makedev(maj, 0); 333 cd->cn_pri = CN_INTERNAL; 334 } 335 336 void 337 ofcons_cninit(cd) 338 struct consdev *cd; 339 { 340 } 341 342 int 343 ofcons_cngetc(dev) 344 dev_t dev; 345 { 346 unsigned char ch = '\0'; 347 int l; 348 349 while ((l = OF_read(stdin, &ch, 1)) != 1) 350 if (l != -2 && l != 0) 351 return -1; 352 return ch; 353 } 354 355 void 356 ofcons_cnputc(dev, c) 357 dev_t dev; 358 int c; 359 { 360 char ch = c; 361 362 OF_write(stdout, &ch, 1); 363 } 364 365 void 366 ofcons_cnpollc(dev, on) 367 dev_t dev; 368 int on; 369 { 370 struct ofcons_softc *sc = ofcons_cd.cd_devs[minor(dev)]; 371 372 if (!sc) 373 return; 374 if (on) { 375 if (sc->of_flags & OFPOLL) 376 callout_stop(&sc->sc_poll_ch); 377 sc->of_flags &= ~OFPOLL; 378 } else { 379 if (!(sc->of_flags & OFPOLL)) { 380 sc->of_flags |= OFPOLL; 381 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 382 } 383 } 384 } 385