1 /* $NetBSD: ofcons.c,v 1.44 2014/03/16 05:20:28 dholland 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.44 2014/03/16 05:20:28 dholland 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 tty *of_tty; 52 struct callout sc_poll_ch; 53 int of_flags; 54 }; 55 /* flags: */ 56 #define OFPOLL 1 57 58 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */ 59 60 cons_decl(ofcons_); 61 62 static int stdin, stdout; 63 64 static int ofcons_match(device_t, cfdata_t, void *); 65 static void ofcons_attach(device_t, device_t, void *); 66 67 CFATTACH_DECL_NEW(ofcons, sizeof(struct ofcons_softc), 68 ofcons_match, ofcons_attach, NULL, NULL); 69 70 extern struct cfdriver ofcons_cd; 71 72 dev_type_open(ofcons_open); 73 dev_type_close(ofcons_close); 74 dev_type_read(ofcons_read); 75 dev_type_write(ofcons_write); 76 dev_type_ioctl(ofcons_ioctl); 77 dev_type_tty(ofcons_tty); 78 dev_type_poll(ofcons_poll); 79 80 const struct cdevsw ofcons_cdevsw = { 81 .d_open = ofcons_open, 82 .d_close = ofcons_close, 83 .d_read = ofcons_read, 84 .d_write = ofcons_write, 85 .d_ioctl = ofcons_ioctl, 86 .d_stop = nostop, 87 .d_tty = ofcons_tty, 88 .d_poll = ofcons_poll, 89 .d_mmap = nommap, 90 .d_kqfilter = ttykqfilter, 91 .d_flag = D_TTY 92 }; 93 94 static int ofcons_probe(void); 95 96 static int 97 ofcons_match(device_t parent, cfdata_t match, void *aux) 98 { 99 struct ofbus_attach_args *oba = aux; 100 101 if (strcmp(oba->oba_busname, "ofw")) 102 return (0); 103 if (!ofcons_probe()) 104 return 0; 105 return OF_instance_to_package(stdin) == oba->oba_phandle 106 || OF_instance_to_package(stdout) == oba->oba_phandle; 107 } 108 109 static void 110 ofcons_attach(device_t parent, device_t self, void *aux) 111 { 112 struct ofcons_softc *sc = device_private(self); 113 114 printf("\n"); 115 116 callout_init(&sc->sc_poll_ch, 0); 117 } 118 119 static void ofcons_start(struct tty *); 120 static int ofcons_param(struct tty *, struct termios *); 121 static void ofcons_pollin(void *); 122 123 int 124 ofcons_open(dev_t dev, int flag, int mode, struct lwp *l) 125 { 126 struct ofcons_softc *sc; 127 struct tty *tp; 128 129 sc = device_lookup_private(&ofcons_cd, minor(dev)); 130 if (!sc) 131 return ENXIO; 132 if (!(tp = sc->of_tty)) 133 sc->of_tty = tp = tty_alloc(); 134 tp->t_oproc = ofcons_start; 135 tp->t_param = ofcons_param; 136 tp->t_dev = dev; 137 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 138 return (EBUSY); 139 if (!(tp->t_state & TS_ISOPEN)) { 140 ttychars(tp); 141 tp->t_iflag = TTYDEF_IFLAG; 142 tp->t_oflag = TTYDEF_OFLAG; 143 tp->t_cflag = TTYDEF_CFLAG; 144 tp->t_lflag = TTYDEF_LFLAG; 145 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 146 ofcons_param(tp, &tp->t_termios); 147 ttsetwater(tp); 148 } 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, ofcons_pollin, sc); 154 } 155 156 return (*tp->t_linesw->l_open)(dev, tp); 157 } 158 159 int 160 ofcons_close(dev_t dev, int flag, int mode, struct lwp *l) 161 { 162 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, 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 ofcons_read(dev_t dev, struct uio *uio, int flag) 174 { 175 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, 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 ofcons_write(dev_t dev, struct uio *uio, int flag) 183 { 184 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, 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 ofcons_poll(dev_t dev, int events, struct lwp *l) 192 { 193 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev)); 194 struct tty *tp = sc->of_tty; 195 196 return ((*tp->t_linesw->l_poll)(tp, events, l)); 197 } 198 int 199 ofcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 200 { 201 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev)); 202 struct tty *tp = sc->of_tty; 203 int error; 204 205 if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != EPASSTHROUGH) 206 return error; 207 return ttioctl(tp, cmd, data, flag, l); 208 } 209 210 struct tty * 211 ofcons_tty(dev_t dev) 212 { 213 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev)); 214 215 return sc->of_tty; 216 } 217 218 static void 219 ofcons_start(struct tty *tp) 220 { 221 int s, len; 222 u_char buf[OFBURSTLEN]; 223 224 s = spltty(); 225 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 226 splx(s); 227 return; 228 } 229 tp->t_state |= TS_BUSY; 230 splx(s); 231 len = q_to_b(&tp->t_outq, buf, OFBURSTLEN); 232 OF_write(stdout, buf, len); 233 s = spltty(); 234 tp->t_state &= ~TS_BUSY; 235 if (ttypull(tp)) { 236 tp->t_state |= TS_TIMEOUT; 237 callout_schedule(&tp->t_rstrt_ch, 1); 238 } 239 splx(s); 240 } 241 242 static int 243 ofcons_param(struct tty *tp, struct termios *t) 244 { 245 tp->t_ispeed = t->c_ispeed; 246 tp->t_ospeed = t->c_ospeed; 247 tp->t_cflag = t->c_cflag; 248 return 0; 249 } 250 251 static void 252 ofcons_pollin(void *aux) 253 { 254 struct ofcons_softc *sc = aux; 255 struct tty *tp = sc->of_tty; 256 char ch; 257 258 while (OF_read(stdin, &ch, 1) > 0) { 259 if (tp && (tp->t_state & TS_ISOPEN)) 260 (*tp->t_linesw->l_rint)(ch, tp); 261 } 262 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 263 } 264 265 static int 266 ofcons_probe(void) 267 { 268 int chosen; 269 char stdinbuf[4], stdoutbuf[4]; 270 271 if (stdin) 272 return 1; 273 if ((chosen = OF_finddevice("/chosen")) == -1) 274 return 0; 275 if (OF_getprop(chosen, "stdin", stdinbuf, sizeof stdinbuf) != 276 sizeof stdinbuf || 277 OF_getprop(chosen, "stdout", stdoutbuf, sizeof stdoutbuf) != 278 sizeof stdoutbuf) 279 return 0; 280 281 /* Decode properties. */ 282 stdin = of_decode_int(stdinbuf); 283 stdout = of_decode_int(stdoutbuf); 284 285 return 1; 286 } 287 288 void 289 ofcons_cnprobe(struct consdev *cd) 290 { 291 int maj; 292 293 if (!ofcons_probe()) 294 return; 295 296 maj = cdevsw_lookup_major(&ofcons_cdevsw); 297 cd->cn_dev = makedev(maj, 0); 298 cd->cn_pri = CN_INTERNAL; 299 } 300 301 void 302 ofcons_cninit(struct consdev *cd) 303 { 304 } 305 306 int 307 ofcons_cngetc(dev_t dev) 308 { 309 unsigned char ch = '\0'; 310 int l; 311 312 while ((l = OF_read(stdin, &ch, 1)) != 1) 313 if (l != -2 && l != 0) 314 return -1; 315 return ch; 316 } 317 318 void 319 ofcons_cnputc(dev_t dev, int c) 320 { 321 char ch = c; 322 323 OF_write(stdout, &ch, 1); 324 } 325 326 void 327 ofcons_cnpollc(dev_t dev, int on) 328 { 329 struct ofcons_softc *sc = device_lookup_private(&ofcons_cd, minor(dev)); 330 331 if (!sc) 332 return; 333 if (on) { 334 if (sc->of_flags & OFPOLL) 335 callout_stop(&sc->sc_poll_ch); 336 sc->of_flags &= ~OFPOLL; 337 } else { 338 if (!(sc->of_flags & OFPOLL)) { 339 sc->of_flags |= OFPOLL; 340 callout_reset(&sc->sc_poll_ch, 1, ofcons_pollin, sc); 341 } 342 } 343 } 344