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 29a82b25f9SDavid E. O'Brien #include "opt_ofw.h" 30761f89f9SHartmut Brandt 31707fed20SBenno Rice #include <sys/param.h> 328cabb94fSMarcel Moolenaar #include <sys/kdb.h> 33707fed20SBenno Rice #include <sys/kernel.h> 3443593547SRobert Watson #include <sys/priv.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 51bc093719SEd Schouten static tsw_open_t ofwtty_open; 52bc093719SEd Schouten static tsw_close_t ofwtty_close; 53bc093719SEd Schouten static tsw_outwakeup_t ofwtty_outwakeup; 54707fed20SBenno Rice 55bc093719SEd Schouten static struct ttydevsw ofw_ttydevsw = { 56bc093719SEd Schouten .tsw_flags = TF_NOPREFIX, 57bc093719SEd Schouten .tsw_open = ofwtty_open, 58bc093719SEd Schouten .tsw_close = ofwtty_close, 59bc093719SEd Schouten .tsw_outwakeup = ofwtty_outwakeup, 60707fed20SBenno Rice }; 61707fed20SBenno Rice 62707fed20SBenno Rice static int polltime; 63707fed20SBenno Rice static struct callout_handle ofw_timeouthandle 64707fed20SBenno Rice = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle); 65707fed20SBenno Rice 664cf75455SRobert Watson #if defined(KDB) 67761f89f9SHartmut Brandt static int alt_break_state; 68761f89f9SHartmut Brandt #endif 69761f89f9SHartmut Brandt 70707fed20SBenno Rice static void ofw_timeout(void *); 71707fed20SBenno Rice 7205c3592eSPoul-Henning Kamp static cn_probe_t ofw_cnprobe; 7305c3592eSPoul-Henning Kamp static cn_init_t ofw_cninit; 7405c3592eSPoul-Henning Kamp static cn_term_t ofw_cnterm; 7505c3592eSPoul-Henning Kamp static cn_getc_t ofw_cngetc; 7605c3592eSPoul-Henning Kamp static cn_putc_t ofw_cnputc; 779976156fSAndriy Gapon static cn_grab_t ofw_cngrab; 789976156fSAndriy Gapon static cn_ungrab_t ofw_cnungrab; 79707fed20SBenno Rice 8077dfeeadSPoul-Henning Kamp CONSOLE_DRIVER(ofw); 81707fed20SBenno Rice 8247a1c915SJake Burkholder static void 8347a1c915SJake Burkholder cn_drvinit(void *unused) 8447a1c915SJake Burkholder { 85a121cb6aSJake Burkholder phandle_t options; 86a121cb6aSJake Burkholder char output[32]; 87bc093719SEd Schouten struct tty *tp; 8847a1c915SJake Burkholder 89278667afSJake Burkholder if (ofw_consdev.cn_pri != CN_DEAD && 90278667afSJake Burkholder ofw_consdev.cn_name[0] != '\0') { 91*22b256dfSNathan Whitehorn tp = tty_alloc(&ofw_ttydevsw, NULL); 92*22b256dfSNathan Whitehorn tty_makedev(tp, NULL, "%s", "ofwcons"); 93*22b256dfSNathan Whitehorn 94c5c5a2adSPoul-Henning Kamp /* 95c5c5a2adSPoul-Henning Kamp * XXX: This is a hack and it may result in two /dev/ttya 96c5c5a2adSPoul-Henning Kamp * XXX: devices on platforms where the sab driver works. 97c5c5a2adSPoul-Henning Kamp */ 98*22b256dfSNathan Whitehorn if ((options = OF_finddevice("/options")) == -1 || 99*22b256dfSNathan Whitehorn OF_getprop(options, "output-device", output, 100*22b256dfSNathan Whitehorn sizeof(output)) == -1) 101*22b256dfSNathan Whitehorn return; 102*22b256dfSNathan Whitehorn if (strlen(output) > 0) 103*22b256dfSNathan Whitehorn tty_makealias(tp, output); 104a121cb6aSJake Burkholder } 10547a1c915SJake Burkholder } 10647a1c915SJake Burkholder 107237fdd78SRobert Watson 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 113bc093719SEd Schouten ofwtty_open(struct tty *tp) 114707fed20SBenno Rice { 115a82b25f9SDavid E. O'Brien polltime = hz / OFWCONS_POLL_HZ; 116bc093719SEd Schouten if (polltime < 1) 117707fed20SBenno Rice polltime = 1; 118707fed20SBenno Rice 119707fed20SBenno Rice ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 120bc093719SEd Schouten 121bc093719SEd Schouten return (0); 122707fed20SBenno Rice } 123707fed20SBenno Rice 124bc093719SEd Schouten static void 125bc093719SEd Schouten ofwtty_close(struct tty *tp) 126707fed20SBenno Rice { 127707fed20SBenno Rice 128a82b25f9SDavid E. O'Brien /* XXX Should be replaced with callout_stop(9) */ 129a82b25f9SDavid E. O'Brien untimeout(ofw_timeout, tp, ofw_timeouthandle); 130707fed20SBenno Rice } 131707fed20SBenno Rice 132707fed20SBenno Rice static void 133bc093719SEd Schouten ofwtty_outwakeup(struct tty *tp) 134707fed20SBenno Rice { 135a82b25f9SDavid E. O'Brien int len; 136a82b25f9SDavid E. O'Brien u_char buf[OFBURSTLEN]; 137707fed20SBenno Rice 138bc093719SEd Schouten for (;;) { 139bc093719SEd Schouten len = ttydisc_getc(tp, buf, sizeof buf); 140bc093719SEd Schouten if (len == 0) 141bc093719SEd Schouten break; 142a82b25f9SDavid E. O'Brien OF_write(stdout, buf, len); 143707fed20SBenno Rice } 144707fed20SBenno Rice } 145707fed20SBenno Rice 146707fed20SBenno Rice static void 147707fed20SBenno Rice ofw_timeout(void *v) 148707fed20SBenno Rice { 149707fed20SBenno Rice struct tty *tp; 150707fed20SBenno Rice int c; 151707fed20SBenno Rice 152707fed20SBenno Rice tp = (struct tty *)v; 153707fed20SBenno Rice 154bc093719SEd Schouten tty_lock(tp); 155bc093719SEd Schouten while ((c = ofw_cngetc(NULL)) != -1) 156bc093719SEd Schouten ttydisc_rint(tp, c, 0); 157bc093719SEd Schouten ttydisc_rint_done(tp); 158bc093719SEd Schouten tty_unlock(tp); 159707fed20SBenno Rice 160707fed20SBenno Rice ofw_timeouthandle = timeout(ofw_timeout, tp, polltime); 161707fed20SBenno Rice } 162707fed20SBenno Rice 163707fed20SBenno Rice static void 16477dfeeadSPoul-Henning Kamp ofw_cnprobe(struct consdev *cp) 165707fed20SBenno Rice { 166707fed20SBenno Rice int chosen; 167707fed20SBenno Rice 168707fed20SBenno Rice if ((chosen = OF_finddevice("/chosen")) == -1) { 169707fed20SBenno Rice cp->cn_pri = CN_DEAD; 170707fed20SBenno Rice return; 171707fed20SBenno Rice } 172707fed20SBenno Rice 173707fed20SBenno Rice if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) { 174707fed20SBenno Rice cp->cn_pri = CN_DEAD; 175707fed20SBenno Rice return; 176707fed20SBenno Rice } 177707fed20SBenno Rice 178707fed20SBenno Rice if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) { 179707fed20SBenno Rice cp->cn_pri = CN_DEAD; 180707fed20SBenno Rice return; 181707fed20SBenno Rice } 182707fed20SBenno Rice 1835dbb0622SJake Burkholder cp->cn_pri = CN_LOW; 184707fed20SBenno Rice } 185707fed20SBenno Rice 186707fed20SBenno Rice static void 18705c3592eSPoul-Henning Kamp ofw_cninit(struct consdev *cp) 188707fed20SBenno Rice { 189707fed20SBenno Rice 1902f06c3e7SPoul-Henning Kamp /* XXX: This is the alias, but that should be good enough */ 191c8978106SEd Schouten strcpy(cp->cn_name, "ofwcons"); 192707fed20SBenno Rice } 193707fed20SBenno Rice 19405c3592eSPoul-Henning Kamp static void 19577dfeeadSPoul-Henning Kamp ofw_cnterm(struct consdev *cp) 196707fed20SBenno Rice { 197707fed20SBenno Rice } 198707fed20SBenno Rice 1999976156fSAndriy Gapon static void 2009976156fSAndriy Gapon ofw_cngrab(struct consdev *cp) 2019976156fSAndriy Gapon { 2029976156fSAndriy Gapon } 2039976156fSAndriy Gapon 2049976156fSAndriy Gapon static void 2059976156fSAndriy Gapon ofw_cnungrab(struct consdev *cp) 2069976156fSAndriy Gapon { 2079976156fSAndriy Gapon } 2089976156fSAndriy Gapon 209707fed20SBenno Rice static int 21005c3592eSPoul-Henning Kamp ofw_cngetc(struct consdev *cp) 211707fed20SBenno Rice { 212707fed20SBenno Rice unsigned char ch; 213707fed20SBenno Rice 214cbecdd57SJake Burkholder if (OF_read(stdin, &ch, 1) > 0) { 2154cf75455SRobert Watson #if defined(KDB) 2164cf75455SRobert Watson kdb_alt_break(ch, &alt_break_state); 217761f89f9SHartmut Brandt #endif 218707fed20SBenno Rice return (ch); 219707fed20SBenno Rice } 220707fed20SBenno Rice 221707fed20SBenno Rice return (-1); 222707fed20SBenno Rice } 223707fed20SBenno Rice 224707fed20SBenno Rice static void 22505c3592eSPoul-Henning Kamp ofw_cnputc(struct consdev *cp, int c) 226707fed20SBenno Rice { 227707fed20SBenno Rice char cbuf; 228707fed20SBenno Rice 229707fed20SBenno Rice if (c == '\n') { 230707fed20SBenno Rice cbuf = '\r'; 231707fed20SBenno Rice OF_write(stdout, &cbuf, 1); 232707fed20SBenno Rice } 233707fed20SBenno Rice 234707fed20SBenno Rice cbuf = c; 235707fed20SBenno Rice OF_write(stdout, &cbuf, 1); 236707fed20SBenno Rice } 237