1 /*- 2 * Copyright (C) 2001 Benno Rice. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include "opt_comconsole.h" 30 #include "opt_ofw.h" 31 32 #include <sys/param.h> 33 #include <sys/kdb.h> 34 #include <sys/kernel.h> 35 #include <sys/systm.h> 36 #include <sys/types.h> 37 #include <sys/conf.h> 38 #include <sys/cons.h> 39 #include <sys/consio.h> 40 #include <sys/tty.h> 41 42 #include <dev/ofw/openfirm.h> 43 44 #include <ddb/ddb.h> 45 46 #ifndef OFWCONS_POLL_HZ 47 #define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */ 48 #endif 49 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */ 50 51 static d_open_t ofw_dev_open; 52 static d_close_t ofw_dev_close; 53 54 static struct cdevsw ofw_cdevsw = { 55 .d_version = D_VERSION, 56 .d_open = ofw_dev_open, 57 .d_close = ofw_dev_close, 58 .d_name = "ofw", 59 .d_flags = D_TTY | D_NEEDGIANT, 60 }; 61 62 static struct tty *ofw_tp = NULL; 63 static int polltime; 64 static struct callout_handle ofw_timeouthandle 65 = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle); 66 67 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 68 static int alt_break_state; 69 #endif 70 71 static void ofw_tty_start(struct tty *); 72 static int ofw_tty_param(struct tty *, struct termios *); 73 static void ofw_tty_stop(struct tty *, int); 74 static void ofw_timeout(void *); 75 76 static cn_probe_t ofw_cnprobe; 77 static cn_init_t ofw_cninit; 78 static cn_term_t ofw_cnterm; 79 static cn_getc_t ofw_cngetc; 80 static cn_putc_t ofw_cnputc; 81 82 CONSOLE_DRIVER(ofw); 83 84 static void 85 cn_drvinit(void *unused) 86 { 87 phandle_t options; 88 char output[32]; 89 struct cdev *dev; 90 91 if (ofw_consdev.cn_pri != CN_DEAD && 92 ofw_consdev.cn_name[0] != '\0') { 93 if ((options = OF_finddevice("/options")) == -1 || 94 OF_getprop(options, "output-device", output, 95 sizeof(output)) == -1) 96 return; 97 /* 98 * XXX: This is a hack and it may result in two /dev/ttya 99 * XXX: devices on platforms where the sab driver works. 100 */ 101 dev = make_dev(&ofw_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "%s", 102 output); 103 make_dev_alias(dev, "ofwcons"); 104 } 105 } 106 107 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL) 108 109 static int stdin; 110 static int stdout; 111 112 static int 113 ofw_dev_open(struct cdev *dev, int flag, int mode, struct thread *td) 114 { 115 struct tty *tp; 116 int unit; 117 int error, setuptimeout; 118 119 error = 0; 120 setuptimeout = 0; 121 unit = minor(dev); 122 123 /* 124 * XXX: BAD, should happen at attach time 125 */ 126 if (dev->si_tty == NULL) { 127 ofw_tp = ttyalloc(); 128 dev->si_tty = ofw_tp; 129 ofw_tp->t_dev = dev; 130 } 131 tp = dev->si_tty; 132 133 tp->t_oproc = ofw_tty_start; 134 tp->t_param = ofw_tty_param; 135 tp->t_stop = ofw_tty_stop; 136 tp->t_dev = dev; 137 138 if ((tp->t_state & TS_ISOPEN) == 0) { 139 tp->t_state |= TS_CARR_ON; 140 ttyconsolemode(tp, 0); 141 142 setuptimeout = 1; 143 } else if ((tp->t_state & TS_XCLUDE) && suser(td)) { 144 return (EBUSY); 145 } 146 147 error = ttyld_open(tp, dev); 148 149 if (error == 0 && setuptimeout) { 150 polltime = hz / OFWCONS_POLL_HZ; 151 if (polltime < 1) { 152 polltime = 1; 153 } 154 155 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 156 } 157 158 return (error); 159 } 160 161 static int 162 ofw_dev_close(struct cdev *dev, int flag, int mode, struct thread *td) 163 { 164 int unit; 165 struct tty *tp; 166 167 unit = minor(dev); 168 tp = dev->si_tty; 169 170 if (unit != 0) { 171 return (ENXIO); 172 } 173 174 /* XXX Should be replaced with callout_stop(9) */ 175 untimeout(ofw_timeout, tp, ofw_timeouthandle); 176 ttyld_close(tp, flag); 177 tty_close(tp); 178 179 return (0); 180 } 181 182 183 static int 184 ofw_tty_param(struct tty *tp, struct termios *t) 185 { 186 187 return (0); 188 } 189 190 static void 191 ofw_tty_start(struct tty *tp) 192 { 193 struct clist *cl; 194 int len; 195 u_char buf[OFBURSTLEN]; 196 197 198 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) 199 return; 200 201 tp->t_state |= TS_BUSY; 202 cl = &tp->t_outq; 203 len = q_to_b(cl, buf, OFBURSTLEN); 204 OF_write(stdout, buf, len); 205 tp->t_state &= ~TS_BUSY; 206 207 ttwwakeup(tp); 208 } 209 210 static void 211 ofw_tty_stop(struct tty *tp, int flag) 212 { 213 214 if (tp->t_state & TS_BUSY) { 215 if ((tp->t_state & TS_TTSTOP) == 0) { 216 tp->t_state |= TS_FLUSH; 217 } 218 } 219 } 220 221 static void 222 ofw_timeout(void *v) 223 { 224 struct tty *tp; 225 int c; 226 227 tp = (struct tty *)v; 228 229 while ((c = ofw_cngetc(NULL)) != -1) { 230 if (tp->t_state & TS_ISOPEN) { 231 ttyld_rint(tp, c); 232 } 233 } 234 235 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 236 } 237 238 static void 239 ofw_cnprobe(struct consdev *cp) 240 { 241 int chosen; 242 243 if ((chosen = OF_finddevice("/chosen")) == -1) { 244 cp->cn_pri = CN_DEAD; 245 return; 246 } 247 248 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) { 249 cp->cn_pri = CN_DEAD; 250 return; 251 } 252 253 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) { 254 cp->cn_pri = CN_DEAD; 255 return; 256 } 257 258 cp->cn_pri = CN_LOW; 259 } 260 261 static void 262 ofw_cninit(struct consdev *cp) 263 { 264 265 /* XXX: This is the alias, but that should be good enough */ 266 sprintf(cp->cn_name, "ofwcons"); 267 cp->cn_tp = ofw_tp; 268 } 269 270 static void 271 ofw_cnterm(struct consdev *cp) 272 { 273 } 274 275 static int 276 ofw_cngetc(struct consdev *cp) 277 { 278 unsigned char ch; 279 280 if (OF_read(stdin, &ch, 1) > 0) { 281 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 282 if (kdb_alt_break(ch, &alt_break_state)) 283 kdb_enter("Break sequence on console"); 284 #endif 285 return (ch); 286 } 287 288 return (-1); 289 } 290 291 static void 292 ofw_cnputc(struct consdev *cp, int c) 293 { 294 char cbuf; 295 296 if (c == '\n') { 297 cbuf = '\r'; 298 OF_write(stdout, &cbuf, 1); 299 } 300 301 cbuf = c; 302 OF_write(stdout, &cbuf, 1); 303 } 304