1098ca2bdSWarner Losh /*- 2707fed20SBenno Rice * Copyright (C) 2001 Benno Rice. 3707fed20SBenno Rice * All rights reserved. 4707fed20SBenno Rice * 5707fed20SBenno Rice * Redistribution and use in source and binary forms, with or without 6707fed20SBenno Rice * modification, are permitted provided that the following conditions 7707fed20SBenno Rice * are met: 8707fed20SBenno Rice * 1. Redistributions of source code must retain the above copyright 9707fed20SBenno Rice * notice, this list of conditions and the following disclaimer. 10707fed20SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright 11707fed20SBenno Rice * notice, this list of conditions and the following disclaimer in the 12707fed20SBenno Rice * documentation and/or other materials provided with the distribution. 13707fed20SBenno Rice * 14707fed20SBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15707fed20SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16707fed20SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17707fed20SBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18707fed20SBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19707fed20SBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20707fed20SBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21707fed20SBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22707fed20SBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23707fed20SBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24707fed20SBenno Rice */ 25707fed20SBenno Rice 268368cf8fSDavid E. O'Brien #include <sys/cdefs.h> 278368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$"); 28707fed20SBenno Rice 29761f89f9SHartmut Brandt #include "opt_comconsole.h" 30a82b25f9SDavid E. O'Brien #include "opt_ofw.h" 31761f89f9SHartmut Brandt 32707fed20SBenno Rice #include <sys/param.h> 338cabb94fSMarcel Moolenaar #include <sys/kdb.h> 34707fed20SBenno Rice #include <sys/kernel.h> 35707fed20SBenno Rice #include <sys/systm.h> 36707fed20SBenno Rice #include <sys/types.h> 37707fed20SBenno Rice #include <sys/conf.h> 38707fed20SBenno Rice #include <sys/cons.h> 39707fed20SBenno Rice #include <sys/consio.h> 40707fed20SBenno Rice #include <sys/tty.h> 41707fed20SBenno Rice 42707fed20SBenno Rice #include <dev/ofw/openfirm.h> 43707fed20SBenno Rice 44761f89f9SHartmut Brandt #include <ddb/ddb.h> 45761f89f9SHartmut Brandt 46a82b25f9SDavid E. O'Brien #ifndef OFWCONS_POLL_HZ 47a82b25f9SDavid E. O'Brien #define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */ 48a82b25f9SDavid E. O'Brien #endif 49a82b25f9SDavid E. O'Brien #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */ 50707fed20SBenno Rice 51707fed20SBenno Rice static d_open_t ofw_dev_open; 52707fed20SBenno Rice static d_close_t ofw_dev_close; 53707fed20SBenno Rice 54707fed20SBenno Rice static struct cdevsw ofw_cdevsw = { 55dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 567ac40f5fSPoul-Henning Kamp .d_open = ofw_dev_open, 577ac40f5fSPoul-Henning Kamp .d_close = ofw_dev_close, 587ac40f5fSPoul-Henning Kamp .d_name = "ofw", 59dc08ffecSPoul-Henning Kamp .d_flags = D_TTY | D_NEEDGIANT, 60707fed20SBenno Rice }; 61707fed20SBenno Rice 62707fed20SBenno Rice static struct tty *ofw_tp = NULL; 63707fed20SBenno Rice static int polltime; 64707fed20SBenno Rice static struct callout_handle ofw_timeouthandle 65707fed20SBenno Rice = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle); 66707fed20SBenno Rice 678cabb94fSMarcel Moolenaar #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 68761f89f9SHartmut Brandt static int alt_break_state; 69761f89f9SHartmut Brandt #endif 70761f89f9SHartmut Brandt 71707fed20SBenno Rice static void ofw_tty_start(struct tty *); 72707fed20SBenno Rice static int ofw_tty_param(struct tty *, struct termios *); 73707fed20SBenno Rice static void ofw_tty_stop(struct tty *, int); 74707fed20SBenno Rice static void ofw_timeout(void *); 75707fed20SBenno Rice 7605c3592eSPoul-Henning Kamp static cn_probe_t ofw_cnprobe; 7705c3592eSPoul-Henning Kamp static cn_init_t ofw_cninit; 7805c3592eSPoul-Henning Kamp static cn_term_t ofw_cnterm; 7905c3592eSPoul-Henning Kamp static cn_getc_t ofw_cngetc; 8005c3592eSPoul-Henning Kamp static cn_putc_t ofw_cnputc; 81707fed20SBenno Rice 8277dfeeadSPoul-Henning Kamp CONSOLE_DRIVER(ofw); 83707fed20SBenno Rice 8447a1c915SJake Burkholder static void 8547a1c915SJake Burkholder cn_drvinit(void *unused) 8647a1c915SJake Burkholder { 87a121cb6aSJake Burkholder phandle_t options; 88a121cb6aSJake Burkholder char output[32]; 8989c9c53dSPoul-Henning Kamp struct cdev *dev; 9047a1c915SJake Burkholder 91278667afSJake Burkholder if (ofw_consdev.cn_pri != CN_DEAD && 92278667afSJake Burkholder ofw_consdev.cn_name[0] != '\0') { 93a121cb6aSJake Burkholder if ((options = OF_finddevice("/options")) == -1 || 94a121cb6aSJake Burkholder OF_getprop(options, "output-device", output, 95a121cb6aSJake Burkholder sizeof(output)) == -1) 96a121cb6aSJake Burkholder return; 97c5c5a2adSPoul-Henning Kamp /* 98c5c5a2adSPoul-Henning Kamp * XXX: This is a hack and it may result in two /dev/ttya 99c5c5a2adSPoul-Henning Kamp * XXX: devices on platforms where the sab driver works. 100c5c5a2adSPoul-Henning Kamp */ 1013f99f14bSPoul-Henning Kamp dev = make_dev(&ofw_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "%s", 102984e2655SJake Burkholder output); 1033f99f14bSPoul-Henning Kamp make_dev_alias(dev, "ofwcons"); 104a121cb6aSJake Burkholder } 10547a1c915SJake Burkholder } 10647a1c915SJake Burkholder 107c9c7976fSPoul-Henning Kamp SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL) 10847a1c915SJake Burkholder 109707fed20SBenno Rice static int stdin; 110707fed20SBenno Rice static int stdout; 111707fed20SBenno Rice 112707fed20SBenno Rice static int 11389c9c53dSPoul-Henning Kamp ofw_dev_open(struct cdev *dev, int flag, int mode, struct thread *td) 114707fed20SBenno Rice { 115707fed20SBenno Rice struct tty *tp; 116707fed20SBenno Rice int unit; 117707fed20SBenno Rice int error, setuptimeout; 118707fed20SBenno Rice 119707fed20SBenno Rice error = 0; 120707fed20SBenno Rice setuptimeout = 0; 121707fed20SBenno Rice unit = minor(dev); 122707fed20SBenno Rice 123c5c5a2adSPoul-Henning Kamp /* 124c5c5a2adSPoul-Henning Kamp * XXX: BAD, should happen at attach time 125c5c5a2adSPoul-Henning Kamp */ 126c5c5a2adSPoul-Henning Kamp if (dev->si_tty == NULL) { 127c5c5a2adSPoul-Henning Kamp ofw_tp = ttyalloc(); 128c5c5a2adSPoul-Henning Kamp dev->si_tty = ofw_tp; 129c5c5a2adSPoul-Henning Kamp ofw_tp->t_dev = dev; 130c5c5a2adSPoul-Henning Kamp } 131c5c5a2adSPoul-Henning Kamp tp = dev->si_tty; 132707fed20SBenno Rice 133707fed20SBenno Rice tp->t_oproc = ofw_tty_start; 134707fed20SBenno Rice tp->t_param = ofw_tty_param; 135707fed20SBenno Rice tp->t_stop = ofw_tty_stop; 136707fed20SBenno Rice tp->t_dev = dev; 137707fed20SBenno Rice 138707fed20SBenno Rice if ((tp->t_state & TS_ISOPEN) == 0) { 139707fed20SBenno Rice tp->t_state |= TS_CARR_ON; 14095bc5689SPoul-Henning Kamp ttyconsolemode(tp, 0); 141707fed20SBenno Rice 142707fed20SBenno Rice setuptimeout = 1; 14344731cabSJohn Baldwin } else if ((tp->t_state & TS_XCLUDE) && suser(td)) { 144707fed20SBenno Rice return (EBUSY); 145707fed20SBenno Rice } 146707fed20SBenno Rice 1472140d01bSPoul-Henning Kamp error = ttyld_open(tp, dev); 148707fed20SBenno Rice 149707fed20SBenno Rice if (error == 0 && setuptimeout) { 150a82b25f9SDavid E. O'Brien polltime = hz / OFWCONS_POLL_HZ; 151707fed20SBenno Rice if (polltime < 1) { 152707fed20SBenno Rice polltime = 1; 153707fed20SBenno Rice } 154707fed20SBenno Rice 155707fed20SBenno Rice ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 156707fed20SBenno Rice } 157707fed20SBenno Rice 158707fed20SBenno Rice return (error); 159707fed20SBenno Rice } 160707fed20SBenno Rice 161707fed20SBenno Rice static int 16289c9c53dSPoul-Henning Kamp ofw_dev_close(struct cdev *dev, int flag, int mode, struct thread *td) 163707fed20SBenno Rice { 164707fed20SBenno Rice int unit; 165707fed20SBenno Rice struct tty *tp; 166707fed20SBenno Rice 167707fed20SBenno Rice unit = minor(dev); 168c5c5a2adSPoul-Henning Kamp tp = dev->si_tty; 169707fed20SBenno Rice 170707fed20SBenno Rice if (unit != 0) { 171707fed20SBenno Rice return (ENXIO); 172707fed20SBenno Rice } 173707fed20SBenno Rice 174a82b25f9SDavid E. O'Brien /* XXX Should be replaced with callout_stop(9) */ 175a82b25f9SDavid E. O'Brien untimeout(ofw_timeout, tp, ofw_timeouthandle); 1762140d01bSPoul-Henning Kamp ttyld_close(tp, flag); 177672c05d4SPoul-Henning Kamp tty_close(tp); 178707fed20SBenno Rice 179707fed20SBenno Rice return (0); 180707fed20SBenno Rice } 181707fed20SBenno Rice 182707fed20SBenno Rice 183707fed20SBenno Rice static int 184707fed20SBenno Rice ofw_tty_param(struct tty *tp, struct termios *t) 185707fed20SBenno Rice { 186707fed20SBenno Rice 187707fed20SBenno Rice return (0); 188707fed20SBenno Rice } 189707fed20SBenno Rice 190707fed20SBenno Rice static void 191707fed20SBenno Rice ofw_tty_start(struct tty *tp) 192707fed20SBenno Rice { 193a82b25f9SDavid E. O'Brien struct clist *cl; 194a82b25f9SDavid E. O'Brien int len; 195a82b25f9SDavid E. O'Brien u_char buf[OFBURSTLEN]; 196707fed20SBenno Rice 197a82b25f9SDavid E. O'Brien 198a82b25f9SDavid E. O'Brien if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) 199707fed20SBenno Rice return; 200707fed20SBenno Rice 201707fed20SBenno Rice tp->t_state |= TS_BUSY; 202a82b25f9SDavid E. O'Brien cl = &tp->t_outq; 203a82b25f9SDavid E. O'Brien len = q_to_b(cl, buf, OFBURSTLEN); 204a82b25f9SDavid E. O'Brien OF_write(stdout, buf, len); 205707fed20SBenno Rice tp->t_state &= ~TS_BUSY; 206707fed20SBenno Rice 207707fed20SBenno Rice ttwwakeup(tp); 208707fed20SBenno Rice } 209707fed20SBenno Rice 210707fed20SBenno Rice static void 211707fed20SBenno Rice ofw_tty_stop(struct tty *tp, int flag) 212707fed20SBenno Rice { 213707fed20SBenno Rice 214707fed20SBenno Rice if (tp->t_state & TS_BUSY) { 215707fed20SBenno Rice if ((tp->t_state & TS_TTSTOP) == 0) { 216707fed20SBenno Rice tp->t_state |= TS_FLUSH; 217707fed20SBenno Rice } 218707fed20SBenno Rice } 219707fed20SBenno Rice } 220707fed20SBenno Rice 221707fed20SBenno Rice static void 222707fed20SBenno Rice ofw_timeout(void *v) 223707fed20SBenno Rice { 224707fed20SBenno Rice struct tty *tp; 225707fed20SBenno Rice int c; 226707fed20SBenno Rice 227707fed20SBenno Rice tp = (struct tty *)v; 228707fed20SBenno Rice 22977dfeeadSPoul-Henning Kamp while ((c = ofw_cngetc(NULL)) != -1) { 230707fed20SBenno Rice if (tp->t_state & TS_ISOPEN) { 2312140d01bSPoul-Henning Kamp ttyld_rint(tp, c); 232707fed20SBenno Rice } 233707fed20SBenno Rice } 234707fed20SBenno Rice 235707fed20SBenno Rice ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 236707fed20SBenno Rice } 237707fed20SBenno Rice 238707fed20SBenno Rice static void 23977dfeeadSPoul-Henning Kamp ofw_cnprobe(struct consdev *cp) 240707fed20SBenno Rice { 241707fed20SBenno Rice int chosen; 242707fed20SBenno Rice 243707fed20SBenno Rice if ((chosen = OF_finddevice("/chosen")) == -1) { 244707fed20SBenno Rice cp->cn_pri = CN_DEAD; 245707fed20SBenno Rice return; 246707fed20SBenno Rice } 247707fed20SBenno Rice 248707fed20SBenno Rice if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) { 249707fed20SBenno Rice cp->cn_pri = CN_DEAD; 250707fed20SBenno Rice return; 251707fed20SBenno Rice } 252707fed20SBenno Rice 253707fed20SBenno Rice if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) { 254707fed20SBenno Rice cp->cn_pri = CN_DEAD; 255707fed20SBenno Rice return; 256707fed20SBenno Rice } 257707fed20SBenno Rice 2585dbb0622SJake Burkholder cp->cn_pri = CN_LOW; 259707fed20SBenno Rice } 260707fed20SBenno Rice 261707fed20SBenno Rice static void 26205c3592eSPoul-Henning Kamp ofw_cninit(struct consdev *cp) 263707fed20SBenno Rice { 264707fed20SBenno Rice 2652f06c3e7SPoul-Henning Kamp /* XXX: This is the alias, but that should be good enough */ 2662f06c3e7SPoul-Henning Kamp sprintf(cp->cn_name, "ofwcons"); 267a121cb6aSJake Burkholder cp->cn_tp = ofw_tp; 268707fed20SBenno Rice } 269707fed20SBenno Rice 27005c3592eSPoul-Henning Kamp static void 27177dfeeadSPoul-Henning Kamp ofw_cnterm(struct consdev *cp) 272707fed20SBenno Rice { 273707fed20SBenno Rice } 274707fed20SBenno Rice 275707fed20SBenno Rice static int 27605c3592eSPoul-Henning Kamp ofw_cngetc(struct consdev *cp) 277707fed20SBenno Rice { 278707fed20SBenno Rice unsigned char ch; 279707fed20SBenno Rice 280cbecdd57SJake Burkholder if (OF_read(stdin, &ch, 1) > 0) { 2818cabb94fSMarcel Moolenaar #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER) 2828cabb94fSMarcel Moolenaar if (kdb_alt_break(ch, &alt_break_state)) 2838cabb94fSMarcel Moolenaar kdb_enter("Break sequence on console"); 284761f89f9SHartmut Brandt #endif 285707fed20SBenno Rice return (ch); 286707fed20SBenno Rice } 287707fed20SBenno Rice 288707fed20SBenno Rice return (-1); 289707fed20SBenno Rice } 290707fed20SBenno Rice 291707fed20SBenno Rice static void 29205c3592eSPoul-Henning Kamp ofw_cnputc(struct consdev *cp, int c) 293707fed20SBenno Rice { 294707fed20SBenno Rice char cbuf; 295707fed20SBenno Rice 296707fed20SBenno Rice if (c == '\n') { 297707fed20SBenno Rice cbuf = '\r'; 298707fed20SBenno Rice OF_write(stdout, &cbuf, 1); 299707fed20SBenno Rice } 300707fed20SBenno Rice 301707fed20SBenno Rice cbuf = c; 302707fed20SBenno Rice OF_write(stdout, &cbuf, 1); 303707fed20SBenno Rice } 304