1 1.3 dholland /* $NetBSD: romcons.c,v 1.3 2014/07/25 08:10:34 dholland Exp $ */ 2 1.1 tsutsui 3 1.1 tsutsui /* 4 1.1 tsutsui * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 1.1 tsutsui * Copyright (C) 1995, 1996 TooLs GmbH. 6 1.1 tsutsui * All rights reserved. 7 1.1 tsutsui * 8 1.1 tsutsui * Redistribution and use in source and binary forms, with or without 9 1.1 tsutsui * modification, are permitted provided that the following conditions 10 1.1 tsutsui * are met: 11 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright 12 1.1 tsutsui * notice, this list of conditions and the following disclaimer. 13 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the 15 1.1 tsutsui * documentation and/or other materials provided with the distribution. 16 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software 17 1.1 tsutsui * must display the following acknowledgement: 18 1.1 tsutsui * This product includes software developed by TooLs GmbH. 19 1.1 tsutsui * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 1.1 tsutsui * derived from this software without specific prior written permission. 21 1.1 tsutsui * 22 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 tsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 tsutsui * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 1.1 tsutsui * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 1.1 tsutsui * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 1.1 tsutsui * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 1.1 tsutsui * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 1.1 tsutsui * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 1.1 tsutsui * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 tsutsui */ 33 1.1 tsutsui /* 34 1.1 tsutsui * romcons.c - from sys/dev/ofw/ofcons.c 35 1.1 tsutsui */ 36 1.1 tsutsui 37 1.1 tsutsui #include <sys/cdefs.h> 38 1.3 dholland __KERNEL_RCSID(0, "$NetBSD: romcons.c,v 1.3 2014/07/25 08:10:34 dholland Exp $"); 39 1.1 tsutsui 40 1.1 tsutsui #include <sys/param.h> 41 1.1 tsutsui #include <sys/conf.h> 42 1.1 tsutsui #include <sys/device.h> 43 1.1 tsutsui #include <sys/proc.h> 44 1.1 tsutsui #include <sys/systm.h> 45 1.1 tsutsui #include <sys/callout.h> 46 1.1 tsutsui #include <sys/tty.h> 47 1.1 tsutsui #include <sys/kauth.h> 48 1.1 tsutsui 49 1.1 tsutsui #include <dev/cons.h> 50 1.1 tsutsui 51 1.1 tsutsui #include <machine/autoconf.h> 52 1.1 tsutsui #include <machine/romcall.h> 53 1.1 tsutsui 54 1.1 tsutsui #include "ioconf.h" 55 1.1 tsutsui 56 1.1 tsutsui struct romcons_softc { 57 1.1 tsutsui device_t sc_dev; 58 1.1 tsutsui struct tty *sc_tty; 59 1.1 tsutsui struct callout sc_poll_ch; 60 1.1 tsutsui int sc_flags; 61 1.1 tsutsui #define CONS_POLL 1 62 1.1 tsutsui }; 63 1.1 tsutsui 64 1.1 tsutsui #define BURSTLEN 128 /* max number of bytes to write in one chunk */ 65 1.1 tsutsui 66 1.1 tsutsui cons_decl(romcons_); 67 1.1 tsutsui 68 1.1 tsutsui static int romcons_match(device_t, cfdata_t, void *); 69 1.1 tsutsui static void romcons_attach(device_t, device_t, void *); 70 1.1 tsutsui 71 1.1 tsutsui CFATTACH_DECL_NEW(romcons, sizeof(struct romcons_softc), 72 1.1 tsutsui romcons_match, romcons_attach, NULL, NULL); 73 1.1 tsutsui 74 1.1 tsutsui dev_type_open(romcons_open); 75 1.1 tsutsui dev_type_close(romcons_close); 76 1.1 tsutsui dev_type_read(romcons_read); 77 1.1 tsutsui dev_type_write(romcons_write); 78 1.1 tsutsui dev_type_ioctl(romcons_ioctl); 79 1.1 tsutsui dev_type_tty(romcons_tty); 80 1.1 tsutsui dev_type_poll(romcons_poll); 81 1.1 tsutsui 82 1.1 tsutsui void romcons_kbdinput(int); 83 1.1 tsutsui 84 1.1 tsutsui const struct cdevsw romcons_cdevsw = { 85 1.2 dholland .d_open = romcons_open, 86 1.2 dholland .d_close = romcons_close, 87 1.2 dholland .d_read = romcons_read, 88 1.2 dholland .d_write = romcons_write, 89 1.2 dholland .d_ioctl = romcons_ioctl, 90 1.2 dholland .d_stop = nostop, 91 1.2 dholland .d_tty = romcons_tty, 92 1.2 dholland .d_poll = romcons_poll, 93 1.2 dholland .d_mmap = nommap, 94 1.2 dholland .d_kqfilter = ttykqfilter, 95 1.3 dholland .d_discard = nodiscard, 96 1.2 dholland .d_flag = D_TTY 97 1.1 tsutsui }; 98 1.1 tsutsui 99 1.1 tsutsui struct consdev consdev_rom = cons_init(romcons_); 100 1.1 tsutsui 101 1.1 tsutsui bool romcons_is_console; 102 1.1 tsutsui 103 1.1 tsutsui static int 104 1.1 tsutsui romcons_match(device_t parent, cfdata_t match, void *aux) 105 1.1 tsutsui { 106 1.1 tsutsui struct mainbus_attach_args *ma = aux; 107 1.1 tsutsui static bool romcons_matched; 108 1.1 tsutsui 109 1.1 tsutsui if (strcmp(ma->ma_name, "romcons")) 110 1.1 tsutsui return 0; 111 1.1 tsutsui 112 1.1 tsutsui if (!romcons_is_console) 113 1.1 tsutsui return 0; 114 1.1 tsutsui 115 1.1 tsutsui if (romcons_matched) 116 1.1 tsutsui return 0; 117 1.1 tsutsui 118 1.1 tsutsui romcons_matched = true; 119 1.1 tsutsui return 1; 120 1.1 tsutsui } 121 1.1 tsutsui 122 1.1 tsutsui static void 123 1.1 tsutsui romcons_attach(device_t parent, device_t self, void *aux) 124 1.1 tsutsui { 125 1.1 tsutsui struct romcons_softc *sc = device_private(self); 126 1.1 tsutsui 127 1.1 tsutsui sc->sc_dev = self; 128 1.1 tsutsui vectab[46] = romcallvec; /* XXX */ 129 1.1 tsutsui aprint_normal("\n"); 130 1.1 tsutsui 131 1.1 tsutsui callout_init(&sc->sc_poll_ch, 0); 132 1.1 tsutsui } 133 1.1 tsutsui 134 1.1 tsutsui static void romcons_start(struct tty *); 135 1.1 tsutsui static int romcons_param(struct tty *, struct termios *); 136 1.1 tsutsui static void romcons_pollin(void *); 137 1.1 tsutsui 138 1.1 tsutsui int 139 1.1 tsutsui romcons_open(dev_t dev, int flag, int mode, struct lwp *l) 140 1.1 tsutsui { 141 1.1 tsutsui struct romcons_softc *sc; 142 1.1 tsutsui struct tty *tp; 143 1.1 tsutsui 144 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 145 1.1 tsutsui if (sc == NULL) 146 1.1 tsutsui return ENXIO; 147 1.1 tsutsui if ((tp = sc->sc_tty) == 0) 148 1.1 tsutsui sc->sc_tty = tp = tty_alloc(); 149 1.1 tsutsui tp->t_oproc = romcons_start; 150 1.1 tsutsui tp->t_param = romcons_param; 151 1.1 tsutsui tp->t_dev = dev; 152 1.1 tsutsui if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 153 1.1 tsutsui return EBUSY; 154 1.1 tsutsui if ((tp->t_state & TS_ISOPEN) == 0) { 155 1.1 tsutsui ttychars(tp); 156 1.1 tsutsui tp->t_iflag = TTYDEF_IFLAG; 157 1.1 tsutsui tp->t_oflag = TTYDEF_OFLAG; 158 1.1 tsutsui tp->t_cflag = TTYDEF_CFLAG; 159 1.1 tsutsui tp->t_lflag = TTYDEF_LFLAG; 160 1.1 tsutsui tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 161 1.1 tsutsui romcons_param(tp, &tp->t_termios); 162 1.1 tsutsui ttsetwater(tp); 163 1.1 tsutsui } 164 1.1 tsutsui tp->t_state |= TS_CARR_ON; 165 1.1 tsutsui 166 1.1 tsutsui if ((sc->sc_flags & CONS_POLL) == 0) { 167 1.1 tsutsui sc->sc_flags |= CONS_POLL; 168 1.1 tsutsui callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc); 169 1.1 tsutsui } 170 1.1 tsutsui 171 1.1 tsutsui return (*tp->t_linesw->l_open)(dev, tp); 172 1.1 tsutsui } 173 1.1 tsutsui 174 1.1 tsutsui int 175 1.1 tsutsui romcons_close(dev_t dev, int flag, int mode, struct lwp *l) 176 1.1 tsutsui { 177 1.1 tsutsui struct romcons_softc *sc; 178 1.1 tsutsui struct tty *tp; 179 1.1 tsutsui 180 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 181 1.1 tsutsui tp = sc->sc_tty; 182 1.1 tsutsui callout_stop(&sc->sc_poll_ch); 183 1.1 tsutsui sc->sc_flags &= ~CONS_POLL; 184 1.1 tsutsui (*tp->t_linesw->l_close)(tp, flag); 185 1.1 tsutsui ttyclose(tp); 186 1.1 tsutsui return 0; 187 1.1 tsutsui } 188 1.1 tsutsui 189 1.1 tsutsui int 190 1.1 tsutsui romcons_read(dev_t dev, struct uio *uio, int flag) 191 1.1 tsutsui { 192 1.1 tsutsui struct romcons_softc *sc; 193 1.1 tsutsui struct tty *tp; 194 1.1 tsutsui 195 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 196 1.1 tsutsui tp = sc->sc_tty; 197 1.1 tsutsui 198 1.1 tsutsui return (*tp->t_linesw->l_read)(tp, uio, flag); 199 1.1 tsutsui } 200 1.1 tsutsui 201 1.1 tsutsui int 202 1.1 tsutsui romcons_write(dev_t dev, struct uio *uio, int flag) 203 1.1 tsutsui { 204 1.1 tsutsui struct romcons_softc *sc; 205 1.1 tsutsui struct tty *tp; 206 1.1 tsutsui 207 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 208 1.1 tsutsui tp = sc->sc_tty; 209 1.1 tsutsui return (*tp->t_linesw->l_write)(tp, uio, flag); 210 1.1 tsutsui } 211 1.1 tsutsui 212 1.1 tsutsui int 213 1.1 tsutsui romcons_poll(dev_t dev, int events, struct lwp *l) 214 1.1 tsutsui { 215 1.1 tsutsui struct romcons_softc *sc; 216 1.1 tsutsui struct tty *tp; 217 1.1 tsutsui 218 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 219 1.1 tsutsui tp = sc->sc_tty; 220 1.1 tsutsui return (*tp->t_linesw->l_poll)(tp, events, l); 221 1.1 tsutsui } 222 1.1 tsutsui int 223 1.1 tsutsui romcons_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 224 1.1 tsutsui { 225 1.1 tsutsui struct romcons_softc *sc; 226 1.1 tsutsui struct tty *tp; 227 1.1 tsutsui int error; 228 1.1 tsutsui 229 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 230 1.1 tsutsui tp = sc->sc_tty; 231 1.1 tsutsui if ((error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l)) != 232 1.1 tsutsui EPASSTHROUGH) 233 1.1 tsutsui return error; 234 1.1 tsutsui return ttioctl(tp, cmd, data, flag, l); 235 1.1 tsutsui } 236 1.1 tsutsui 237 1.1 tsutsui struct tty * 238 1.1 tsutsui romcons_tty(dev_t dev) 239 1.1 tsutsui { 240 1.1 tsutsui struct romcons_softc *sc; 241 1.1 tsutsui 242 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 243 1.1 tsutsui return sc->sc_tty; 244 1.1 tsutsui } 245 1.1 tsutsui 246 1.1 tsutsui static void 247 1.1 tsutsui romcons_start(struct tty *tp) 248 1.1 tsutsui { 249 1.1 tsutsui int s, len; 250 1.1 tsutsui uint8_t buf[BURSTLEN]; 251 1.1 tsutsui 252 1.1 tsutsui s = spltty(); 253 1.1 tsutsui if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) { 254 1.1 tsutsui splx(s); 255 1.1 tsutsui return; 256 1.1 tsutsui } 257 1.1 tsutsui tp->t_state |= TS_BUSY; 258 1.1 tsutsui splx(s); 259 1.1 tsutsui len = q_to_b(&tp->t_outq, buf, BURSTLEN); 260 1.1 tsutsui s = splhigh(); 261 1.1 tsutsui rom_write(1, buf, len); 262 1.1 tsutsui splx(s); 263 1.1 tsutsui s = spltty(); 264 1.1 tsutsui tp->t_state &= ~TS_BUSY; 265 1.1 tsutsui if (ttypull(tp)) { 266 1.1 tsutsui tp->t_state |= TS_TIMEOUT; 267 1.1 tsutsui callout_schedule(&tp->t_rstrt_ch, 1); 268 1.1 tsutsui } 269 1.1 tsutsui splx(s); 270 1.1 tsutsui } 271 1.1 tsutsui 272 1.1 tsutsui static int 273 1.1 tsutsui romcons_param(struct tty *tp, struct termios *t) 274 1.1 tsutsui { 275 1.1 tsutsui 276 1.1 tsutsui tp->t_ispeed = t->c_ispeed; 277 1.1 tsutsui tp->t_ospeed = t->c_ospeed; 278 1.1 tsutsui tp->t_cflag = t->c_cflag; 279 1.1 tsutsui return 0; 280 1.1 tsutsui } 281 1.1 tsutsui 282 1.1 tsutsui static void 283 1.1 tsutsui romcons_pollin(void *aux) 284 1.1 tsutsui { 285 1.1 tsutsui struct romcons_softc *sc = aux; 286 1.1 tsutsui struct tty *tp = sc->sc_tty; 287 1.1 tsutsui char ch; 288 1.1 tsutsui int rv; 289 1.1 tsutsui 290 1.1 tsutsui while (0 && (rv = rom_read(1, &ch, 1)) > 0) { 291 1.1 tsutsui if (tp && (tp->t_state & TS_ISOPEN)) 292 1.1 tsutsui (*tp->t_linesw->l_rint)(ch, tp); 293 1.1 tsutsui } 294 1.1 tsutsui callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc); 295 1.1 tsutsui } 296 1.1 tsutsui 297 1.1 tsutsui void 298 1.1 tsutsui romcons_kbdinput(int ks) 299 1.1 tsutsui { 300 1.1 tsutsui struct romcons_softc *sc; 301 1.1 tsutsui struct tty *tp; 302 1.1 tsutsui 303 1.1 tsutsui sc = device_lookup_private(&romcons_cd, 0); 304 1.1 tsutsui tp = sc->sc_tty; 305 1.1 tsutsui if (tp && (tp->t_state & TS_ISOPEN)) 306 1.1 tsutsui (*tp->t_linesw->l_rint)(ks, tp); 307 1.1 tsutsui } 308 1.1 tsutsui 309 1.1 tsutsui void 310 1.1 tsutsui romcons_cnprobe(struct consdev *cd) 311 1.1 tsutsui { 312 1.1 tsutsui } 313 1.1 tsutsui 314 1.1 tsutsui void 315 1.1 tsutsui romcons_cninit(struct consdev *cd) 316 1.1 tsutsui { 317 1.1 tsutsui int maj; 318 1.1 tsutsui 319 1.1 tsutsui maj = cdevsw_lookup_major(&romcons_cdevsw); 320 1.1 tsutsui cd->cn_dev = makedev(maj, 0); 321 1.1 tsutsui romcons_is_console = true; 322 1.1 tsutsui vectab[46] = romcallvec; /* XXX */ 323 1.1 tsutsui } 324 1.1 tsutsui 325 1.1 tsutsui int 326 1.1 tsutsui romcons_cngetc(dev_t dev) 327 1.1 tsutsui { 328 1.1 tsutsui unsigned char ch = '\0'; 329 1.1 tsutsui int l; 330 1.1 tsutsui 331 1.1 tsutsui while ((l = rom_read(1, &ch, 1)) != 1) 332 1.1 tsutsui if (l != -2 && l != 0) 333 1.1 tsutsui return -1; 334 1.1 tsutsui return ch; 335 1.1 tsutsui } 336 1.1 tsutsui 337 1.1 tsutsui void 338 1.1 tsutsui romcons_cnputc(dev_t dev, int c) 339 1.1 tsutsui { 340 1.1 tsutsui char ch = c; 341 1.1 tsutsui 342 1.1 tsutsui rom_write(1, &ch, 1); 343 1.1 tsutsui } 344 1.1 tsutsui 345 1.1 tsutsui void 346 1.1 tsutsui romcons_cnpollc(dev_t dev, int on) 347 1.1 tsutsui { 348 1.1 tsutsui struct romcons_softc *sc; 349 1.1 tsutsui 350 1.1 tsutsui sc = device_lookup_private(&romcons_cd, minor(dev)); 351 1.1 tsutsui 352 1.1 tsutsui if (sc == NULL) 353 1.1 tsutsui return; 354 1.1 tsutsui if (on) { 355 1.1 tsutsui if (sc->sc_flags & CONS_POLL) 356 1.1 tsutsui callout_stop(&sc->sc_poll_ch); 357 1.1 tsutsui sc->sc_flags &= ~CONS_POLL; 358 1.1 tsutsui } else { 359 1.1 tsutsui if ((sc->sc_flags & CONS_POLL) == 0) { 360 1.1 tsutsui sc->sc_flags |= CONS_POLL; 361 1.1 tsutsui callout_reset(&sc->sc_poll_ch, 1, romcons_pollin, sc); 362 1.1 tsutsui } 363 1.1 tsutsui } 364 1.1 tsutsui } 365